Click here to Skip to main content
15,920,111 members
Home / Discussions / C#
   

C#

 
QuestionDifference Between Equality Operator ( ==) and .Equals() Method in C# Pin
Gyana_Ranjan Dash25-May-15 20:26
Gyana_Ranjan Dash25-May-15 20:26 
AnswerRe: Difference Between Equality Operator ( ==) and .Equals() Method in C# Pin
OriginalGriff25-May-15 20:46
mveOriginalGriff25-May-15 20:46 
GeneralRe: Difference Between Equality Operator ( ==) and .Equals() Method in C# Pin
Agent__00725-May-15 22:43
professionalAgent__00725-May-15 22:43 
GeneralRe: Difference Between Equality Operator ( ==) and .Equals() Method in C# Pin
Alan N25-May-15 23:22
Alan N25-May-15 23:22 
GeneralRe: Difference Between Equality Operator ( ==) and .Equals() Method in C# Pin
Agent__00725-May-15 23:56
professionalAgent__00725-May-15 23:56 
GeneralRe: Difference Between Equality Operator ( ==) and .Equals() Method in C# Pin
Gyana_Ranjan Dash26-May-15 21:08
Gyana_Ranjan Dash26-May-15 21:08 
GeneralRe: Difference Between Equality Operator ( ==) and .Equals() Method in C# Pin
Armugam Indrani28-May-15 22:48
professionalArmugam Indrani28-May-15 22:48 
AnswerRe: Difference Between Equality Operator ( ==) and .Equals() Method in C# PinPopular
harold aptroot26-May-15 1:02
harold aptroot26-May-15 1:02 
There are many differences, I'll list only some of them..

Equals is a virtual function that is present in Object, whereas operator== is a static function. Many of the differences arise from this. For example (not an exhaustive list),
  • there is by default no operator== for a value type, where there is an Equals inherited from Object.
  • you can use Equals on a value that has any generic type, you can use operator== only if the generic type is constrained to be a reference type (and then it will mean reference equality, not any operator== defined by whatever the concrete type was).
  • an overridden Equals must deal with a parameter of type Object, an implemented operator== can decide for itself what types its parameters must have (at least one of them must be the same as the declaring type though).
Together this usually (but not always) means that implementing operator== is confusing for a reference type, but expected and wanted for a value type.

They also have slightly difference meanings in general, operator== can be "whatever makes sense", but Equals should be an equivalence relation, because it (or its cousin, IEquatable<sometype>.Equals) is used all over the place in the standard library and third party libraries with the assumption that it is an equivalence relation. Violating that will cause trouble if, for example, you use it as a key in a Dictionary<Tkey, Tvalue>, or if you try to put it in as HashSet<T> (which various parts LINQ like to do behind the scenes, breaking unexpectedly and invisibly if Equals is not an equivalence relation).

Usually operator== should (for sanity) also be an equivalence relation, but it isn't always, for example take double.NaN, which must be equal to itself according to Equals (equivalence relations must be reflexive), but compares unequal to everything (including itself) according to operator==. If it didn't compare equal to itself according to Equals, putting it in a HashSet<double> would result in it "never really being present" (it would be added, but testing whether it is in there would return false), so thankfully that is not how it works. Meanwhile it should also compare unequal to itself, otherwise it would be in blatant violation of the IEEE 754 standard for floating point arithmetic.
QuestionDataRelation problem Pin
Supratik De25-May-15 6:35
Supratik De25-May-15 6:35 
AnswerRe: DataRelation problem Pin
Richard Deeming27-May-15 1:19
mveRichard Deeming27-May-15 1:19 
QuestionInterface implementation strategies : four techniques; but: when (if) to use (which) one of them Pin
BillWoodruff25-May-15 4:06
professionalBillWoodruff25-May-15 4:06 
AnswerRe: Interface implementation strategies : four techniques; but: when (if) to use (which) one of them Pin
Richard Deeming27-May-15 1:29
mveRichard Deeming27-May-15 1:29 
QuestionToolstrip grey border Pin
Member 1171517124-May-15 6:52
Member 1171517124-May-15 6:52 
AnswerRe: Toolstrip grey border Pin
Ravi Bhavnani25-May-15 9:53
professionalRavi Bhavnani25-May-15 9:53 
QuestionReport From Date to Date Pin
abdo.kouta23-May-15 5:45
abdo.kouta23-May-15 5:45 
AnswerRe: Report From Date to Date Pin
OriginalGriff23-May-15 6:15
mveOriginalGriff23-May-15 6:15 
GeneralRe: Report From Date to Date Pin
abdo.kouta23-May-15 8:02
abdo.kouta23-May-15 8:02 
AnswerRe: Report From Date to Date Pin
Mycroft Holmes23-May-15 17:22
professionalMycroft Holmes23-May-15 17:22 
QuestionRe: Report From Date to Date Pin
abdo.kouta23-May-15 21:37
abdo.kouta23-May-15 21:37 
AnswerRe: Report From Date to Date Pin
Mycroft Holmes24-May-15 0:28
professionalMycroft Holmes24-May-15 0:28 
GeneralRe: Report From Date to Date Pin
abdo.kouta24-May-15 4:02
abdo.kouta24-May-15 4:02 
GeneralRe: Report From Date to Date Pin
Mycroft Holmes24-May-15 14:16
professionalMycroft Holmes24-May-15 14:16 
AnswerRe: Report From Date to Date Pin
jschell24-May-15 7:01
jschell24-May-15 7:01 
Questionasp.net with c# Pin
Member 1158609823-May-15 0:50
Member 1158609823-May-15 0:50 
AnswerRe: asp.net with c# Pin
Sascha Lefèvre23-May-15 1:18
professionalSascha Lefèvre23-May-15 1:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.