When overloading an equality operator is the best way to handle null values?

Possible duplicate:
How to check zeros in overloading operator '== without infinite recursion?

Let's say I have this type:

public class Effect { public static bool operator == ( Effect a, Effect b ) { return a.Equals ( b ); } public static bool operator != ( Effect a, Effect b ) { return !a.Equals ( b ); } public bool Equals ( Effect effect ) { return this.TypeID.Equals ( effect.TypeID ); } public override bool Equals ( object obj ) { return this.TypeID.Equals ( ( ( Effect ) obj ).TypeID ); } } 

What is the most reliable and clean way to handle null values?

I'm not sure if I need to check the null value for both the current instance ( this ) and the past instance (effect/obj) ? If I have zero for the current instance ( this ), will the compiler still call effect.Equals or Object.Equals?

Also in any case, where should the zero checks be performed? I accept only inside Equals methods, not equality operators ( ==, != ).

+4
source share
6 answers

Firstly, this never be null , at least not in code generated by the C # compiler.

Second, use the ReferenceEquals method to check for a null reference without invoking the overloaded version == (or do ((object) sometypeinstance) == null ).

+2
source

Visual Studio has a snippet that provides you with a basic implementation of Equals (). I would follow this if you have no good reason not to.

 // override object.Equals public override bool Equals(object obj) { // // See the full list of guidelines at // http://go.microsoft.com/fwlink/?LinkID=85237 // and also the guidance for operator== at // http://go.microsoft.com/fwlink/?LinkId=85238 // if (obj == null || GetType() != obj.GetType()) { return false; } // TODO: write your implementation of Equals() here throw new NotImplementedException(); return base.Equals(obj); } // override object.GetHashCode public override int GetHashCode() { // TODO: write your implementation of GetHashCode() here throw new NotImplementedException(); return base.GetHashCode(); } 
+2
source

What about

 public static bool operator == ( Effect a, Effect b ) { return object.Equals(a, b); } 

The implementation of the Object.Equals () object by default performs a null check.

If you're interested, here's how Object.Equals () does it (courtesy of .NET Reflector):

 public static bool Equals(object objA, object objB) { return ((objA == objB) || (((objA != null) && (objB != null)) && objA.Equals(objB))); } 
+1
source

You need to check zeros with every method that takes parameters making your class.

 public class Effect { public static bool operator == ( Effect a, Effect b ) { if (a == null) && (b == null) return true; if (a == null) return false; return a.Equals ( b ); } public static bool operator != ( Effect a, Effect b ) { return !(a == b); } public bool Equals ( Effect effect ) { if (b == null) return false; return this.TypeID.Equals ( effect.TypeID ); } public override bool Equals ( object obj ) { if (obj == null) return false; return this.TypeID.Equals ( ( ( Effect ) obj ).TypeID ); } } 

Other things to look for are GetHashCode, which should be implemented if equality holds, and the fact that GetHashCode should only be implemented in immutable properties if this object will be used in a dictionary or similar object that compares elements using a hash -code.

0
source

Add this:

  public static bool operator == ( Effect a, Effect b ) { return a is Effect && b is Effect && a.TypeID.Equals (b.TypeID); } public static bool operator != ( Effect a, Effect b ) { return !(a == b ); } public bool Equals ( Effect effect ) { return this == effect ); } public override bool Equals ( object obj ) { return obj is Effect && this == obj); } 

or instead of the last put:

  public override bool Equals ( object obj ) { if (obj == null) throw new ArgumentNullException( "obj", "obj is null"); if (!(obj is effect)) throw new ArgumentException( "obj", "obj is not an effect"); return obj is Effect && this == obj); } 
-1
source

Yes, you must clear from zero. Why are you afraid of this?

Remember that when you play with peers, you probably also want to take a look at the hashcode method! These two methods are intertwined.

-1
source

Source: https://habr.com/ru/post/1338001/


All Articles