C# - Understand Equality For Value Type And Reference Type

Introduction

In this article, we are going to discuss how equality works with reference type and value type.

Prerequisites

Basic understanding of class and structure in C#.

How does equality work with Reference Type and Value Type?

The first question that comes to mind is, what is a reference type and value type?

Value Type

In value type, the data value is stored in its own memory address. Eg int i= 500.

Examples of value types are structure, int, char, bool, decimal, etc.

Reference Type

In reference type, it stores points of the memory location where the data values are stored.

Eg. string str = “Hello Kirtesh”

An example of a reference type is a class.

Equality for Value Type and Reference Type

Now we will discuss how equality works with a reference type.

Let’s discuss the below code to understand how equality works with reference type (Class) and structure type (Structure),

Reference type Equality

We will use ==, Equal(), or System.Object.ReferenceEquals() for reference and value equality check.

  1. System.Object.ReferenceEquals(): method is used to check if specific object instances are the same instance or not.
  2. ==: Used to check if both objects point to the same location.
  3. Equal(): Method used for ReferenceEquals().

Let’s understand all three concepts using the below example.

public class Member
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
}
class Program
{
    static void Main(string[] args)
    {
        Member obj1 = new Member
        {
            Id = 1,
            Name = "Kirtesh",
            Address = "Vadodara"
        };
        Member obj2 = new Member
        {
            Id = 1,
            Name = "Kirtesh",
            Address = "Vadodara"
        };

        Console.WriteLine("***Two Objects with same Values****");
        Console.WriteLine(obj1 == obj2);
        Console.WriteLine(obj1.Equals(obj2));
        Console.WriteLine(System.Object.ReferenceEquals(obj1, obj2));

        Console.ReadLine();
    }
}

Output

Output object

In the above example, obj1 and obj2 have the same values and different memory locations, and the result is false because it is doing reference equities, not value comparisons.

Now I'm assigning obj2 to obj1 in the below code with different values and checking the output.

class Program
{
    static void Main(string[] args)
    {
        Member obj1 = new Member
        {
            Id = 1,
            Name = "Kirtesh",
            Address = "Vadodara"
        };
        Member obj2 = new Member
        {
            Id = 2,
            Name = "Pankaj",
            Address = "Vadodara"
        };

        obj1 = obj2;

        Console.WriteLine("***Obj1 = obj2****");
        Console.WriteLine(obj1 == obj2);
        Console.WriteLine(obj1.Equals(obj2));
        Console.WriteLine(System.Object.ReferenceEquals(obj1, obj2));
        Console.ReadLine();
    }
}

Output

True object

I hope this example makes reference to equality clear. Now we will check structure equities.

Struct Equality

Let`s see the below code snippet,

public struct MyStruct
{
    public int Id { get; set; }
    public string Name { get; set; }
}
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("*******Struct Comparison***********");
        MyStruct mystr1 = new MyStruct
        {
            id = 1,
            name = "Kirtesh"
        };
        MyStruct mystr2 = new MyStruct
        {
            id = 1,
            name = "Pankaj"
        };

        // Console.WriteLine(mystr1 == mystr2); -- compile time error
        Console.WriteLine(mystr1.Equals(mystr2));
        Console.WriteLine(System.Object.ReferenceEquals(mystr1, mystr2));
        Console.ReadLine();
    }
}

Output

Struct comparision

As values are not the same, hence we got false results. Let's make the value the same for both structs and try again.

class Program
{
    static void Main(string[] args)
    {
        MyStruct mystr1 = new MyStruct
        {
            id = 1,
            name = "Kirtesh"
        };
        MyStruct mystr2 = new MyStruct
        {
            id = 1,
            name = "Kirtesh"
        };

        // Console.WriteLine(mystr1 == mystr2); compiler Error
        Console.WriteLine(mystr1.Equals(mystr2));
        Console.WriteLine(System.Object.ReferenceEquals(mystr1, mystr2));
        Console.ReadLine();
    }
}

Output

Struct comparision both

As values are the same we got true in the first case. But memory location is different in the second scenario, hence the result is false.

I hope this helps you to understand equality for reference and value types.


Similar Articles