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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: