Comparing Objects in .Net – Reference Equality

30-05-2012

Comparing two objects returns true if they point to the same entry of the
heap.

Public Class Person
{
   Private String Name;
   Private int Age;

   Public Person(String Name)
   {
      this.Name = Name;
   }
}

Class Program
{
      public static void Main(String[] args)
      {
         Person P1 = new Person("John");
         Person P2 = new Person("Ron");
         Person P3 = P1;

        Console.WriteLine(P1 == P2);     // Prints False
        Console.WriteLine(P1 == P3);     // Prints True
      }
}

Menol

ILT

Leave a comment