c# - What's the difference between IEquatable and just overriding Object.Equals()? -
i want food
class able test whenever equal instance of food
. later use against list, , want use list.contains()
method. should implement iequatable<food>
or override object.equals()
? msdn:
this method determines equality using default equality comparer, defined object's implementation of iequatable.equals method t (the type of values in list).
so next question is: functions/classes of .net framework make use of object.equals()
? should use in first place?
the main reason performance. when generics introduced in .net 2.0 able add bunch of neat classes such list<t>
, dictionary<k,v>
, hashset<t>
, etc. these structures make heavy use of gethashcode
, equals
. value types required boxing. iequatable<t>
lets structure implement typed equals
method no boxing required. better performance when using value types generic collections.
reference types don't benefit iequatable<t>
implementation let avoid cast system.object
can make difference if it's called frequently.
as noted on jared parson's blog though, still must implement object overrides.
Comments
Post a Comment