C# 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.
Course info
Rating
(596)
Level
Intermediate
Updated
Sep 10, 2014
Duration
4h 51m
Table of contents
Why Is Equality So Hard?
Equality in .NET
The C# Equality Operator
Implementing Equality for Value Types
Implementing Equality for Reference Types
Comparisons in .NET
Equality and Comparisons for Strings
Comparers and Equality Comparers
Hash Codes and Hashtables
Structural Equality and Comparisons
Description
Course info
Rating
(596)
Level
Intermediate
Updated
Sep 10, 2014
Duration
4h 51m
Description

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.

About the author
About the author

Simon is a senior developer with over 20 years programming experience, particularly covering C++ and the .NET Framework. He believes knowledge is to be shared and has written or co-written more than a dozen books for professional programmers.

More from the author
C# Concurrent Collections
Advanced
3h 32m
15 May 2015
C# Collections Fundamentals
Intermediate
5h 56m
8 Apr 2014
Math For Programmers
Intermediate
5h 16m
26 Aug 2013
Section Introduction Transcripts
Section Introduction Transcripts

Equality in .NET
Hello, this is the Pluralsight C# Equality and Comparisons course. I'm Simon Robinson, and in this module, I'm going to explain how. NET handles Equality and Comparisons out-of-the-box. This means you'll start to understand how. NET addresses some of the issues that we saw in the previous module. We'll start by looking in detail at the virtual object. Equals method. This provides arguably the most important mechanism for equality checking in. NET, since it's the means by which any type can say what equality means for itself. I'll show you how, out-of-the-box, this method gives you reference equality for most reference types, but value equality for all value types. We'll also compare this with the static method of the same name, which is more robust against null instances. And I'll talk about how to get guaranteed reference equality using the static ReferenceEquals method. object. Equals suffers from two problems; lack of strong typing and the need to box value types. And I'll end this module with a look at the IEquatable of <T> interface, which provides a solution to those. After watching this module, you should have a good understanding of what equality means in. NET.

Implementing Equality for Value Types
Hello, I'm Simon Robinson, and this is the Pluralsight C# Equality and Comparisons course. This module is about how to override Equality for Value Types. The techniques for implementing equality for value types and reference types are different. So it makes sense to treat them separately. I will focus on value types in this module, and then show you how to do the same thing for reference types in the next module. I'll start by examining why you would want to override equality for a value type, and you'll see that in practice, for value types, doing this is almost always a good idea. Then, I'll demo implementing equality for a struct. You'll see that this requires overriding object. Equals, implementing the IEquatable of <T> interface, implementing equality and inequality overloads, and overriding another method, object. GetHashCode. By the way, about the terminology, when I say Implement Equality or Override Equality, what I mean is -- Define what equality means for a particular type, replacing the equality implementation that. NET would've given you out-of-the-box for that type. It involves, as you've just seen, overriding the object. Equals method and doing a bunch of other stuff. There isn't really an established term used to refer to that process, but I think Override Equality and Implement Equality both convey the right meaning, so I'll use either of those interchangeably in this course.

Implementing Equality for Reference Types
Hello, I'm Simon Robinson, and this is the Pluralsight C# Equality and Comparisons course. This module is about how to Implement Equality for Reference Types. We've covered overriding or implementing equality for value types in the last module. Now we'll see how to do the same thing when we have the added complications of dealing with references, nulls, and most importantly, inheritance. I'll start by looking at the reasons why you might want to implement equality for a reference type. There are fewer reasons to do this than there were for a value type. A large part of this module involves presenting a demo of implementing equality. You'll see that you need to override object. Equals and object. GetHashCode, and supply equality and inequality operator overloads. I'll then spend some time showing you how to deal specifically with inheritance. Along the way, you'll see that implementing IEquatable<T> for reference types is often a bad move. And that you also need to be careful how you implement the equality and inequality operators to avoid bugs involving inheritance.

Comparisons in .NET
Hi there, I'm Simon Robinson, and this is the Pluralsight course on C# Equality and Comparisons. This module is all about less-than or greater-than comparisons. Up to now, we have spent quite a bit of time studying equality. We've seen both how. NET implements equality out-of-the-box, and how to override. NET's default implementation for your own types. We are now going to do the same thing for comparisons. In other words, checking whether something is less-than or greater-than something else. Comparisons are implemented in quite a similar way to equality in. NET, which means we will be able to leverage our knowledge of equality to work through comparisons quite quickly. In this module, I'll begin by reviewing what a comparison is, essentially a way of ordering items, and I'll point out some of the conceptual differences between comparisons and equality. Then I'll show you how. NET provides for comparisons using the IComparable of <T> interface, while C# adds full comparison operators. And, we'll see comparisons in use for integers and strings. Next, I'll show you how to implement comparisons yourself on a type. I'll demonstrate this on a value type and point out some additional issues you need to be aware of for reference types. And finally, I'll show you how you can use the IComparable<T> interface, but in general not the operators in generic code.

Equality and Comparisons for Strings
Hi there. This is the Pluralsight course on C# Equality and Comparisons. I'm Simon Robinson, and in this module I'll investigate the particular issues you face when comparing strings. In particular, I'll start off by looking at what is actually stored in a char. We will see that it doesn't always correspond to what you would intuitively think of as a single character. And that's one of the things that complicates comparing strings. Based on this, I'll point out the two main issues you need to consider when comparing strings - Case sensitivity and the Culture. Then we'll go on to some actual coding. I'll work through some demos that show not only how to do different types of string comparison, but also how the results differ. And I'll give you some recommendations for when you should use each type of comparison. An important issue with strings is that the standard equality or comparison techniques, such as the C# equality operator, don't let you explicitly specify the comparison type. So I'll make sure you understand what kind of comparison will actually be performed in those cases. And I'll finish off the module with a quick look at a completely different topic, string pooling. And I'll point out how this can cause unexpected results from reference Equals on strings.

Comparers and Equality Comparers
Hello, I'm Simon Robinson and this is the Pluralsight C# Equality and Comparisons course. In this module, I will introduce you to custom comparers and equality comparers. I'll start by explaining what comparers and equality comparers are, and why they are so useful. Comparers and equality comparers are used extensively by collections. Arguably, most collections would not be able to function without them. And in this module, I'll use a couple of demos that show you how collections leverage them. I'll start by showing you how to code a comparer. For this demo, I'll use a custom comparer to allow an array to sort Food instances. Then we'll code up an equality comparer and we'll use it to ensure that a hash set of FoodItem collection ignores case when it's comparing the Food items. Next, I'll show you how to obtain a default comparer for a type. You can use a default comparer if you're in a situation that requires a comparer, but you don't want a custom comparer; you just need to access a type's own natural comparisons, or equality comparisons. And finally, I'll show you how, for strings, Microsoft has written a number of string comparers, which you can use to save having to roll out your own ones.

Hash Codes and Hashtables
Hello, I'm Simon Robinson and this is the Pluralsight C# Equality and Comparisons course. In this module, I'll explain what hash codes are and how to implement the GetHashCode method. Being able to do this is a crucial part of implementing equality correctly. I'll start by explaining how hash codes are used in hash tables, and you'll see that hash codes enable hash table-based collections, such as Dictionary<TKey, TValue> to function efficiently. Then I'll explain what the requirements are for a hashing function. In particular, how it needs to be consistent with whatever equality does for each type. There's no single right way to write GetHashCode implementations, but I'll take you through a technique based on using the Exclusive-OR operator. This technique normally gives good results in most situations. And for anyone not familiar with Exclusive-OR, I'll also show you how that operator works, so you can see why it's so useful for combining hash codes.

Structural Equality and Comparisons
Hello, I'm Simon Robinson and this is the Pluralsight C# Equality and Comparisons course. And we've now come to the final module of the course, Structural Equality and Comparisons. Structural Equality and Comparisons are a special type of comparison that can be done on some collections and which can tell you whether collections contain the same elements in the same order. I'll start the module by showing you how to do structural equality using the IStructuralEquatable interface. Then I'll do the same thing with structural comparisons using the IStructuralComparable interface. Unfortunately, there aren't many collections that implement structural comparisons out-of-the-box. I'll point out which collections do support them, which is basically Arrays and Tuples. Finally, since this is the last module of the course, we will finish up with a quick overview of the course, including an overview of the various interfaces you can use for equality and comparisons.