Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
The Equals
method is just a virtual one defined in System.Object
, and overridden
by whichever classes choose to do so. The ==
operator is an operator which can be overloaded
by classes, but which usually has identity behaviour.
For reference types where ==
has not been overloaded, it compares whether two references
refer to the same object - which is exactly what the implementation of Equals
does in
System.Object
.
Value types do not provide an overload for ==
by default.
However, most of the value types provided by the framework provide their
own overload. The default implementation of Equals
for a value
type is provided by ValueType
, and uses reflection
to make the comparison, which makes it significantly slower than a
type-specific implementation normally would be. This implementation also
calls Equals
on pairs of references within the two values
being compared.
However, the main difference between the two types of comparison in
normal use (where you're unlikely to be defining your own value types
very often) is polymorphism. Operators are overloaded, not overridden,
which means that unless the compiler knows to call the more specific
version, it'll just call the identity version. To illustrate that, here's
an example:
using System;
public class Test
{
static void Main()
{
// Create two equal but distinct strings
string a = new string(new char[] {'h', 'e', 'l', 'l', 'o'});
string b = new string(new char[] {'h', 'e', 'l', 'l', 'o'});
Console.WriteLine (a==b);
Console.WriteLine (a.Equals(b));
// Now let's see what happens with the same tests but
// with variables of type object
object c = a;
object d = b;
Console.WriteLine (c==d);
Console.WriteLine (c.Equals(d));
}
}
The results are:
True
True
False
True
The third line is False
because the compiler can only call the non-overloaded version of ==
as it doesn't know that the contents of c
and d
are both string references. As they
are references to different strings, the identity operator returns false.
So, when should you use which operator? My rule of thumb is that for almost all reference types, use Equals
when you want to test equality rather than reference identity. The exception is for strings - comparing strings with
==
does make things an awful lot simpler and more readable but you need to remember that both
sides of the operator must be expressions of type string in order to get the comparison to work properly.
For value types, I'd normally use ==
for easier-to-read code. Things would get tricky if a value type
provided an overload for ==
which acted differently to Equals
, but I'd consider such a type
very badly designed to start with.
[Author: Jon Skeet]
Anonymous
April 16, 2004
I thought that structs do not get an implementation of op== by default.Anonymous
April 18, 2004
Hmm... you're right. I'll amend the article when I'm in a more suitable location...Anonymous
April 20, 2004
Article modified for correctness - thanks Mark.Anonymous
April 24, 2004
if this observation is correct then why this shows equality ?
string a="1";
object b="1";
Console.WriteLine(a==b);
Reply me at vkakkar@sisware.com
Thanks!
VishAnonymous
April 25, 2004
Because string literals are interned - both a and b are references to the same object. Try this code instead:
string a=new string(new char[]{'1'});
object b=new string(new char[]{'1'});
Console.WriteLine(a==b);Anonymous
May 18, 2004
I am a beginner and would greatly appreciate if you could explain this in more detail:
"Because string literals are interned - both a and b are references to the same object."
What is "interned?"Anonymous
May 20, 2004
cool blog. just few lines clear lot of doubts.Anonymous
June 23, 2004
Identity vs EquivalenceAnonymous
June 23, 2004
Identity vs EquivalenceAnonymous
June 23, 2004
Identity vs EquivalenceAnonymous
July 17, 2004
my web:
http://www.sj55.com/pic_sort http://www.zw88.com/paopaotang.htm http://www.zw88.com/sj.htm http://www.zw88.com/sm/
http://www.zw88.com/sms/ http://www.zw88.com/zw.htm http://www.resou.com/8888.htm http://www.sj55.com/pic/pic_1130.htmAnonymous
September 15, 2004
Ping Back来自:blog.csdn.netAnonymous
December 27, 2004
[http://itpeixun.51.net/][http://aissl.51.net/][http://kukuxz003.freewebpage.org/][http://kukuxz001.51.net/][http://kukuxz003.51.net/][http://kukuxz005.51.net/][http://kukuxz002.51.net/][http://kukuxz004.freewebpage.org/][http://kukuxz007.51.net/][http://kukuxz001.freewebpage.org/][http://kukuxz006.51.net/][http://kukuxz002.freewebpage.org/][http://kukuxz004.51.net/][http://kukuxz008.51.net/][http://kukuxz009.51.net/][http://kukuxz005.freewebpage.org/][http://kukuxz006.freewebpage.org/][http://kukuxz007.freewebpage.org/][http://kukuxz009.freewebpage.org/]Anonymous
September 06, 2006
The notion of identity and equivalence is fundamental, yet very confusing at times. You might end upAnonymous
October 29, 2007
.NETFramework类库提供了静态方法publicstaticboolReferenceEquals(Anonymous
November 12, 2007
PingBack from http://protsyk.com/cms/?p=140Anonymous
June 08, 2009
PingBack from http://cellulitecreamsite.info/story.php?id=8552Anonymous
July 03, 2012
This post doesn't mention the fact that the compiler doesn't do type checking with .Equals but it does with ==. e.g. string mystring = "hello"; int num = 1; bool result = mystring.Equals(num); // this compiles OK! bool result2 = mystring == num; // this won't compile - goodAnonymous
November 21, 2012
Very Clear Explanation. Thanks.!Anonymous
May 23, 2013
// Create two equal but distinct strings string a = "1"; //new string(new char[] { 'h', 'e', 'l', 'l', 'o' }); string b = "1"; //new string(new char[] { 'h', 'e', 'l', 'l', 'o' }); Console.WriteLine(a == b); Console.WriteLine(a.Equals(b)); // Now let's see what happens with the same tests but // with variables of type object object c = a; object d = b; Console.WriteLine(c.GetType()); Console.WriteLine(d.GetType()); Console.WriteLine(c == d); Console.WriteLine(c.Equals(d)); Console.ReadKey(); Why all 4 statements are true in this code ? Please explain ...Anonymous
April 09, 2014
check this link ....a nice explanation of this net-informations.com/.../equals.htm steveAnonymous
September 24, 2014
Thanks for this brief yet concise difference.Anonymous
December 13, 2014
For those who are interested, here's a blog which benchmarks the fastest way to compare strings in C#: cc.davelozinski.com/.../fastest-way-to-compare-strings _Anonymous
March 29, 2015
now i'm clear about == operator AND Equals method THANXAnonymous
July 23, 2015
Maybe mention nullables? They are structs, so I assume it's better to use == than Equals().Anonymous
August 20, 2015
Because if two string literals contains same value then it will pooled in same memory location. Hence the below code string a="1"; object b="1"; Console.WriteLine(a==b); output
true. Because both are pointing to same memory location. if you try this below code string a="1"; object b="2"; Console.WriteLine(a==b); output
false. Because both variables point to their own memory location with different values and these values are not pooled.
- Anonymous
January 16, 2016
string a = new string(new char[] { 'h', 'e', 'l', 'l', 'o' }); string b = new string(new char[] { 'h', 'a', 'l', 'l', 'o' }); Console.WriteLine(a==b); Console.WriteLine(a.Equals(b)); object c = a; object d = b; Console.WriteLine(a==b); Console.WriteLine(a.Equals(b)); Try This one... All answer ll be false...