Click here to Skip to main content
15,884,388 members
Articles / Programming Languages / C#

Equality Operator (==) Problem with Inheritance and Generics in C#

Rate me:
Please Sign up or sign in to vote.
4.87/5 (32 votes)
21 Jun 2018CPOL10 min read 32K   26   17
This post focuses on the problems that one might face when using == operator with generics or inheritance and which can cause unexpected results.

Introduction

In this post, we will be seeing why it is not always a good option to use == operator for doing equality, we will be seeing how == operator does not work well with inheritance and generics and will see that the better option is to rely on virtual Equals method.

Background

In the previous post, we learned that == operator does not work for non-primitive value types unless we overload the operator for that value type, and we saw how we can overload the equality operator to enable == operator comparison for non-primitive value types. We also compare the == operator and Object.Equals method to see how their behavior differs for primitive types, value types and reference types.

Comparison of == operator and Object.Equals

Here is the comparison of both:

  • For Primitive Types, e.g., int, float, long, bool, etc., both the == operator and Object.Equals method will compare the values, i.e., 1 is equal to but 1, but 1 is not equal to 0.
  • For most of the Reference Types, both the == operator and Object.Equals method will by default compare the references, you can modify this behavior by overloading the == operator or overriding the Object.Equals method but if you want the behavior of both to be consistent and don't want to surprise other developers and yourself, you must do both (overload == operator and override the Equals method).
  • For Non-primitive Value Types, the Object.Equals method will do the value equality using Refection which is slow and this is overridden behavior of course, but the equality operator is by default not available for value types unless you overload the == operator for that type which we saw in the example above.
  • There is also another minor difference that for reference types the virtual Equals method cannot work if the first parameter is null but that is trivial, as a workaround, the static Equals method can be used which takes both the objects to be compared as parameter.

Previous Posts in the Series

You might want to read the previous posts in this series, if yes, following are the links to the previous content related to it:

Equality Operator and Inheritance Problem

Let's create some example code that will illustrate how == operator behaves when inheritance is in play in our code, so let's declare two string type variables and we will check the result once again by doing an equality check in 4 different ways that are available in C#:

C#
public class Program
{
   public static void Main(string[] args)
   {
        string str = "Ehsan Sajjad";
        string str1 = string.Copy(str);

        Console.WriteLine(ReferenceEquals(str, str1));
        Console.WriteLine(str.Equals(str1));
        Console.WriteLine(str == str1);
        Console.WriteLine(object.Equals(str, str1));
    }
}

Output

The following is the output of the above executed code:

Image 1

First, we check if both string variables have reference to the same string object using ReferenceEquals method, next we check using the instance method Equals of String type, on the third line, we are again checking for equality but this time using the == operator and lastly, we are checking using the static Equals method of Object so that we can compare the results of all these 4 techniques. We should be able to tell what would be the result from the previous posts which we have done so far:

  • ReferenceEquals will for sure return false as both are references to different objects, not the same object.
  • The Equals method of String type will also return true as both strings are identical (i.e., same sequence or characters).
  • == Operator will also return true as both string values are equal.
  • virtual Object.Equals call will also return true as the overridden implementation of String would be called and it checks the equality of values of string.

All the above until now makes sense, now we will modify the above example a little, the only change we will make is instead of using variables of type String, we will use variables of type Object and we will see how the output differs from the above code, here is the code after converting it to use Object type instead of String:

C#
static void Main(string[] args)
{
    Object str = "Ehsan Sajjad";
    Object str1 = string.Copy((string)str);

    Console.WriteLine(ReferenceEquals(str, str1));
    Console.WriteLine(str.Equals(str1));
    Console.WriteLine(str == str1);
    Console.WriteLine(object.Equals(str, str1));
}

Output

Image 2

So we can see that the result is different than what we had before when we were using variables of type String. The other three ways methods are still returning the same result but the == operator equality check is now returning false instead of true stating that the two strings are not equal contradicting with the fact that they are equal actually and it's also conflicting with the result of other two methods.

Why is That?

The reason behind this is that the == operator is equivalent to a static method, and a static method cannot be a virtual method, what is actually happening when comparing using == operator here is that we are trying to compare two variables of type Object, we know that they are of type String in actual, but the compiler is not aware of that, and we know that for non-virtual methods, it is decided at compile-time that which implementation needs to be invoked and as the variables have been declared of type Object the compiler will emit code for comparing instances of type Object and if you navigate the source code for Object you will see that there is no overload for == operator for it, so what will happen here is that == operator will do what it always does for comparing two reference types when there is no overload found for that type (i.e., it will check for reference equality) and as these two string objects are separate instances, the reference equality will be evaluated to false saying that the two objects are not equal.

Object Equals Method Should be Preferred

The above problem will never come with the other two Equals methods as they are virtual and the specific type will provide the override for it and calling them will always call the overridden implementation to correctly evaluate the equality of them, so in the above case, overridden methods of String type will be called.

For static Equals method, we already discussed in one of the previous posts that it internally calls the same virtual Equals method, it is just there where we want to avoid NRE (Null Reference Exception) and there is a chance that the object on which we will be calling Equals method could be null.

So in case of inheritance, Equals method overrides should be preferred when checking for equality instead of using == operator.

== Operator and Reference Equals

Another thing to note is that casting the operands of equality operator to object before comparing will always give the same result as calling the ReferenceEquals method.

Some developers write that way to check for reference equality. We should be careful when doing reference equality that way because someone else reading that code in future might not understand or not be aware that casting to object causes the comparison to be based of reference.

But if we explicitly call the ReferenceEquals method, that would clarify the intent that we want to do reference equality and it would clear all doubts about what the code needs to do.

== Operator and Generics Problem

Another reason to avoid the == operator is if we are using generics in our code. To illustrate that, let's create a simple method which takes two generic parameters of type T and compares to check if both the objects are equal and prints the result on the Console, the code for which would be:

C#
static void Equals<T>(T a, T b)
{
    Console.WriteLine(a == b);
}

The above example is obviously pretty simple and one can easily tell what it is actually doing. We are using equality operator to compare the two objects of type T, but if you try to build the above example in Visual Studio, it wouldn't build and a compile time error would come saying:

Error CS0019 : Operator '==' cannot be applied to operands of type 'T' and 'T'

We are seeing the above error because the T could be any type, it can be a reference type or a value type or a primitive type and there is no guarantee that the type parameter which is being passed provided implementation of == operator for itself.

In C# generics, there is no way to apply a constraint on the generic type or method which could force the past type parameter to provide the overload implementation of the == operator, we can make the above code build successfully by putting the class constraint on type T like:

C#
static void Equals<t>(T a, T b) where T : class
{
    Console.WriteLine(a == b);
}

So we have put a constraint on generic type T to be a reference type due to which we are now able to compile the code successfully, as == operator can always be used on reference types with no problems and it will check for reference equality of two objects of reference type.

Temporary Solution

We are able to build the code now, but there is a problem because what if we need to pass value type parameters on primitive types to the generic Equals method, that we will not be able to do as we had put the restriction to this method to only be called for reference types.

Let's write some code in the main method which would create two identical strings as we did couple of times in previous posts as well:

C#
class Program
{
    static void Main(string[] args)
    {
        Object str = "Ehsan Sajjad";
        Object str1 = string.Copy((string)str);

        Equals(str, str1);
    }

    static void Equals<T>(T a, T b) where T : class
    {
        Console.WriteLine(a == b);
    }
}

So guess what would be the output of the above code when we run it, it will evaluate the two strings to be equal or not? If we recall what we saw before was that for String type, the == operator overload defined by String compares the values of the objects, so the above should print true on the Console, but if we run the code, we will see the opposite result that it printed false:

Image 3

So, the above code has given us unexpected results, we were expecting True to be printed on Console. The question which comes to mind instantly is because why it is evaluating to false, it looks like the == operator is checking the two objects for Reference Equality instead of Value Equality, but the question is why it is doing reference equality.

This is happening because even though the compiler knows that it can apply == operator to whatever the type T being passed, in the above case String and it has == operator overload, but compiler does not know whether the generic type which is being used when using the method overloads the == operator or not, so it assumes that T wouldn't overload it and it compiles the code considering that the == operator is called on instances of type Object which clearly happened that it checked for Reference Equality.

This is very important to keep in mind when using == operator with generics. The == operator will not use the equality operator overload defined by the type T and it will consider it as Object.

Again Object.Equals to Rescue

Now let's change our generic method to use Equals method instead of equality operator which would be:

C#
static void Equals<t>(T a, T b)
{
   Console.WriteLine(object.Equals(a,b));
}

We have removed the class constraint as Object.Equals can be called on any type and we are using the static method for the same reason that if one of the parameters is passed null, our code wouldn't fail and will work as expected, and now this method will work for both value types and reference types.

Now if we run the code again, we will see that it printed the result as expected because the object.Equals will call the appropriate overridden implementation of Equals method at run-time as static method will call the virtual Equals method and we see the expected result True as both string values are equal.

Summary

  • == Operator doesn't work well with Inheritance and might give you unexpected results when used with inheritance because the == operator is not virtual, so wherever possible virtual Equals or static Equals methods should be preferred.
  • == Operator also doesn't work as expected when used in Generic class or methods and Equals method should be used with generic to avoid unexpected behavior in the program.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Pakistan Pakistan
Ehsan Sajjad is a Microsoft Certified Professional, Microsoft Certified C# Specialist and he is also among the top users on StackOverflow from Pakistan with 50k+ reputation at time of writing this and counting.

He is a passionate software developer with around 5 years of professional experience in Microsoft Technologies both web and desktop applications and always open to learn new things and platforms especially in mobile application development and game development.

Some Achievements :

  • 5th Top Contributor from Pakistan on Stackoverflow.com
  • Top User in ASP.NET MVC from Pakistan on Stackoverflow.com
  • 21st June 2017 - Article of the Day - ASP.NET Community (Beginners Guide on AJAX CRUD Operations in Grid using JQuery DataTables in ASP.NET MVC 5)
  • 19th April 2017 - Article of the Day - ASP.NET Community (ASP.NET MVC Async File Uploading using JQuery)
  • March 2017 - Visual C# Technical Guru Silver Medal on Microsoft Tech Net Wiki Article Competition
  • 20 January 2017 - Article of the Day - ASP.NET Community (Async File Uploading in ASP.NET MVC)
  • 22nd September 2016 - Article of the Day - ASP.NET Community (GridView with Server Side Filtering, Sorting and Paging in ASP.NET MVC 5)
  • 22nd August 2016 - Article of the Day - ASP.NET Community (Beginners Guide for Creating GridView in ASP.NET MVC 5)
  • December 2015 - C-SharpCorner Monthly Winner

Comments and Discussions

 
QuestionHere is my take on this issue. Pin
Shams Mukhtar7-Aug-18 11:21
Shams Mukhtar7-Aug-18 11:21 
AnswerRe: Here is my take on this issue. Pin
Ehsan Sajjad26-Nov-18 17:46
professionalEhsan Sajjad26-Nov-18 17:46 
QuestionPolymorphism is the answer to this problem. Pin
AnotherKen23-Jun-18 12:55
professionalAnotherKen23-Jun-18 12:55 
AnswerRe: Polymorphism is the answer to this problem. Pin
Ehsan Sajjad26-Nov-18 17:43
professionalEhsan Sajjad26-Nov-18 17:43 
QuestionNice article, just complaining Pin
Member 1247379523-Jun-18 19:32
Member 1247379523-Jun-18 19:32 
AnswerRe: Nice article, just complaining Pin
Ehsan Sajjad26-Nov-18 17:47
professionalEhsan Sajjad26-Nov-18 17:47 
GeneralMy vote of 2 Pin
GerVenson22-Jun-18 4:14
professionalGerVenson22-Jun-18 4:14 
GeneralRe: My vote of 2 Pin
Member 1024352822-Jun-18 6:35
Member 1024352822-Jun-18 6:35 
GeneralGood Pin
Member 1342871421-Jun-18 3:26
Member 1342871421-Jun-18 3:26 
QuestionLink problem. Pin
Paulo Zemek26-Mar-18 7:47
mvaPaulo Zemek26-Mar-18 7:47 
AnswerRe: Link problem. Pin
Ehsan Sajjad21-Jun-18 2:25
professionalEhsan Sajjad21-Jun-18 2:25 
Questionmy vote of #5 Pin
BillWoodruff18-Jul-17 9:12
professionalBillWoodruff18-Jul-17 9:12 
AnswerRe: my vote of #5 Pin
Ehsan Sajjad18-Jul-17 22:25
professionalEhsan Sajjad18-Jul-17 22:25 
GeneralMy vote of 5 Pin
djmarcus9-May-17 4:34
djmarcus9-May-17 4:34 
GeneralRe: My vote of 5 Pin
Ehsan Sajjad10-May-17 2:33
professionalEhsan Sajjad10-May-17 2:33 
SuggestionUsing EqualityComparer<T>.Default.Equals or Comparer<T>.Default.Compare Pin
Speady4-May-17 10:31
Speady4-May-17 10:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.