Equals in Object Class (Comparing objects)

using System;

namespace Object_Class_ToString
{
class Emp
{
int id;
string name;

public int Id
{
get { return id; }
set { id = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
//public override string ToString()
//{
//    return string.Format(“{0} – {1}”, id,name);
//}
}

class Program
{
static void Main(string[] args)
{
Emp e1 = new Emp();
Emp e2 = e1;
Console.WriteLine(e1.Equals(e2));
//will return true since both refering to same object

Emp e3 = new Emp();//new Object
e2 = e3;
Console.WriteLine(e1.Equals(e2));
//will return false they are refering to different objects
Console.ReadLine();
}
}
}

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s