.net - Object.Equals clarification in C#? -
i've made simple test :
object t = 3; object aa = 3; #1 console.writeline(t.equals(aa)); #2 console.writeline(t.equals(3)); #3 console.writeline(3.equals(aa));
all of them true.(that's problem actually).
looking @ object
, used function:
public virtual bool equals(object obj);
the equals virtual. can overridden.
but don't see polymorphic behavior. pure boxed value.
regarding line #1 t.equals(aa)
the reference type static type - object.
so thought should phone call object.equals
: means reference different , meaning first reply should false
.(and wrong here). why that?
regarding line #2 t.equals(3)
again, t's
static type object. object.equals
running. how come true
?
regarding line #3 3.equals(aa)
i believe public override bool equals(object obj);
running because static type in
t. , param type object. why true
? un-box value ?
it seems , somehow unboxes object without notice :-(
object
s equals
method polymorphic, can overriden subtypes int
. int32.equals overrides method value comparing between current object , argument, , since arguments equal 1 time unboxed, returns true.
there 2 overload of int32.equals
- bool equals(object)
, bool equals(int)
. bool equals(object)
overload 1 overriden object
. since t
, aa
object
references, method called in examples 1 , 2.
in illustration 3, still overload called since aa
object
, hence valid overload.
the ==
operator static , resolved statically based on types of arguments, both object
in example. ==
operator object
compares references, , in case homecoming false 2 separate boxed ints.
c# .net equals
No comments:
Post a Comment