void fun()
{
object o1 = "chirag";
object o2 = new string("chirag".ToCharArray());
object o3 = "chirag";
Console.WriteLine(o1 == o2); // false. both object are different.
Console.WriteLine(o1 == o3); // true . when creating o2 object, fatch data from
//String pool and o2
// get same object as o. becuase string class
// override GetHashCode() method and it give same hash code.
Console.WriteLine(o1.Equals(o2));// true. both object has content are same.
Console.WriteLine(o1.Equals(o3));// true. both object has content are same.
C obj1 = new C(10);
C obj2 = new C(10);
Console.WriteLine(obj1 == obj2); // false
Console.WriteLine(obj1.Equals(obj2)); //false
Console.ReadLine();
}
public class C
{
int id;
public C(int id)
{
this.id = id;
}
}
Output:
FALSE
TRUE
TRUE
TRUE.
-----------------------------------------------------------------------------
“==” compares if the object references are same while “.Equals()” compares if the contents are same.
{
object o1 = "chirag";
object o2 = new string("chirag".ToCharArray());
object o3 = "chirag";
Console.WriteLine(o1 == o2); // false. both object are different.
Console.WriteLine(o1 == o3); // true . when creating o2 object, fatch data from
//String pool and o2
// get same object as o. becuase string class
// override GetHashCode() method and it give same hash code.
Console.WriteLine(o1.Equals(o2));// true. both object has content are same.
Console.WriteLine(o1.Equals(o3));// true. both object has content are same.
C obj1 = new C(10);
C obj2 = new C(10);
Console.WriteLine(obj1 == obj2); // false
Console.WriteLine(obj1.Equals(obj2)); //false
Console.ReadLine();
}
public class C
{
int id;
public C(int id)
{
this.id = id;
}
}
Output:
FALSE
TRUE
TRUE
TRUE.
-----------------------------------------------------------------------------
“==” compares if the object references are same while “.Equals()” compares if the contents are same.
No comments:
Post a Comment