Thursday, August 6, 2009

Difference between a.Equals(b) and a == b

If both a and b are value type then then you will get same result in a.Equals(b) and a == b.
but in case of reference type both behaves differently :
eg.

string a = new string(new char[] {'d', 'a', 'n', 'c', 'e'});
string b = new string(new char[] {'d', 'a', 'n', 'c', 'e'});
object c = a;
object d = b;

Console.WriteLine (a==b);
Console.WriteLine (a.Equals(b));
Console.WriteLine (c==d);
Console.WriteLine (c.Equals(d));
}
}

Output will be;

True
True
False
True

No comments: