Course
Skills
C# 5 Equality and Comparisons
This course teaches you how equality and comparisons function in .NET and the correct way to implement equality and comparisons for your own types.
What you'll learn
In this course, you'll learn how to use equality and comparisons correctly in your C# code: both how they work out of the box, and how to implement equality and comparisons for your own types. The course also covers working with inheritance, string comparisons, writing custom comparers and equality comparers, hash codes, and structural comparisons.
Table of contents
Why Is Equality So Hard?
28mins
- Version Check 0m
- Welcome and Course Overview 4m
- Aims of This Course 2m
- So Why Is Equality Hard? 2m
- Reference vs. Value Equality 4m
- Equality, Value Types, and Boxing 3m
- Multiple Ways to Compare Values 4m
- 'Natural' vs. 'Plugged-In' Equality 3m
- Equality for Floating Points 3m
- The Equality/OOP/Type-Safety Conflict 3m
- Summary 1m
Equality in .NET
27mins
- Overview 1m
- The Virtual Object.Equals() Method 5m
- Equals() for Strings 3m
- Equals() for Value Types 4m
- Object.Equals() - Review 2m
- The Static Equals() Method 3m
- The ReferenceEquals() Method 1m
- Introducing IEquatable<T> 3m
- IEquatable<int> Demo 2m
- IEquatable<T>, Strings and Reference Types 2m
- Summary 2m
The C# Equality Operator
31mins
- Overview 2m
- The == Operator for Integers 2m
- == Under the Hood 4m
- The == Operator for Buttons 3m
- The == Operator for Strings 5m
- The == Operator for Value Types 2m
- Equality for Tuples 3m
- Comparing == and Object.Equals() 2m
- == Doesn't Always Work With Inheritance 3m
- == Never Works With Generics 4m
- Summary 1m
Implementing Equality for Value Types
16mins
Implementing Equality for Reference Types
34mins
- Overview 1m
- Setting Up the Demo 4m
- Why Implement Equality for Reference Types? 3m
- Implementing Equality in the Base Type 5m
- Implementing Equality in the Derived Type 4m
- How NOT to Overload == 5m
- Making == Work With Inheritance 2m
- == and System.Object 2m
- Understanding the Equality/OOP/Type-Safety Conflict 2m
- The Dangers of Implementing IEquatable<T> 2m
- The Importance of Being Sealed 3m
- Summary 1m
Comparisons in .NET
31mins
- Overview 2m
- What Is a Comparison? 3m
- Introducing IComparable<T> and CompareTo() 5m
- IComparable<T> With Integers 1m
- Comparisons vs. Equality in .NET 3m
- Comparisons Are Value Only 2m
- When Should You Implement IComparable<T>? 3m
- Implementing IComparable<T> Demo 3m
- Implementing the Comparison Operators 3m
- Implementing the Non-Generic IComparable Interface 3m
- IComparable<T> for Reference Types 2m
- IComparable<T> in Generic Code 2m
- Summary 1m
Equality and Comparisons for Strings
37mins
- Overview 1m
- Chars and Unicode Code Points 3m
- Character Expansions 2m
- Surrogates 2m
- Combining Characters 2m
- The Static String.Compare() Method 2m
- Ordinal Comparisons 2m
- Culture-Sensitive Comparisons 1m
- Current Culture vs. Invariant Culture 1m
- Setting Up the String.Compare() Demos 2m
- Uppercase/Lowercase Demo 3m
- Character Expansion Demo 2m
- Combining Character Demo 1m
- Choosing a StringComparison Value 2m
- Choosing a Compare Method 2m
- Equality Comparisons for Strings 2m
- String Pooling and Interning 5m
- Summary 2m
Comparers and Equality Comparers
42mins
- Overview 1m
- Why Do We Need Comparers? 4m
- Comparers and IComparer<T> 1m
- Writing a Comparer 3m
- The Comparer<T> Base Class 3m
- Comparers as Singletons 1m
- The Inconsistent Sort Order Problem 4m
- The Problem of Inheritance (Again) 3m
- IComparer<T> vs. IComparable<T> 2m
- Equality Comparers and IEqualityComparer<T> 1m
- How Collections Leverage Equality Comparers 4m
- Implementing an Equality Comparer 4m
- Implementing IEqualityComparer<T>.Equals() 1m
- Implementing IEqualityComparer<T>.GetHashCode() 1m
- Plugging the Equality Comparer Into a Collection 1m
- Default Comparers and Equality Comparers 4m
- The StringComparer Type 3m
- Summary 2m
Hash Codes and Hashtables
25mins
- Overview 1m
- What Is a Hashtable? 3m
- How Hashtables Use GetHashCode() 4m
- Equal Objects Must Return the Same Hash Code 3m
- Other GetHashCode() Requirements 1m
- A Basic Hash Code Algorithm 2m
- Exclusive OR for Hash Codes 3m
- When Fields Are Not Used in Equality 3m
- Mapping Strings for Hash Codes 3m
- Putting the Hash Code Algorithm Together 1m
- Summary 1m