c# - Using an object in a single return line without declaration -


here piece of code

public override bool equals(object obj)         {             var myobj= obj myclass;             return obj == null || myobj== null || !referenceequals(this, obj) ? false :                 (this.v1== myobj.v1) && (this.v2== myobj.v2) && (this.v3== myobj.v3);         } 

is possible use myobj in return line without declaring above?

thank you!

first of answer "yes", that's not sign. after doing cast, if going use result of cast several times, it's expected thing need put kind of temporary variable.

in code presented, whole expression before ? can simplified referenceequals(this, obj) since reference-equals should safe nulls.

also, since you're doing reference-equals, , return false when fails, don't need other checks. right-side of : superfluous. activates when ref-equals returns true, such case means 2 variables under comparison same object - other comparisons return true anyways - no need them.

so.. whole code reduces to

return referenceequals(this, obj); 

..at least that's how current expression works.

due "it-all-reduces-to-just-referenceequality" think didn't want way.

i think wanted check if ref-equal , return true quickly. if not ref-equal, you'd perform piece-wise comparisons of inner properties. like:

if(refequals(a,b)) return true; if((a==null) != (b==null)) return false; return a.prop1==b.prop1 && a.prop2 == b.prop2 && ... ; 

.. , can compact one-liner, what's point? compacting makes harder read, there's no real performance gain, packs 1 line not gain in terms of "length of text" since can "fold" function in ides...

(btw. in code final line a/b/null-safe refequals taht catch case of 2 nulls , tricky second line catches cases of 1-sided null)


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -