开发者

Overriding == and != on a class

开发者 https://www.devze.com 2023-02-13 16:02 出处:网络
is there any way to do this on a class and still retain the capability to detect whether a variable of the Type is null ? I can do this for structs, (cause struct can\'t really be null, with a struct,

is there any way to do this on a class and still retain the capability to detect whether a variable of the Type is null ? I can do this for structs, (cause struct can't really be null, with a struct, instead of comparing x with null, you use a separate method caleld IsNull or HasValue... but you can't use that technique with a class cause when the class variable is null, of course, you can't call such an instance method on it !

public class myClass: IEquatable<myClass>
{
    public int IntValue { get; set; }
    public static bool operator ==(myClass cA, myClass  cB)
    { return cA is myClass && cB is myClass && cB.Equals(cA); }
    public static bool operator !=(myClass cA, myClass cB)
    { return !(cA == cB); }       
    public override bool Equals(object o) 
    { return o is myClass && this == (myClass)o; }
    public override bool Equals(myClass o) 
    { return IntValue == o.IntValue; }

} 

but when I go:

myClass x;
if (x != null)    // this returns false when it should be true 
{
   //code to execute with x here
}

For what it's worth, the only reason I want this to be a class is because it participates in a simple inheritance relationship. This is an immutable class, and in effect what I am trying to do here is code it so it can behave like an immut开发者_StackOverflow中文版able and nullable class, exactly like an immutable, nullable struct behaves (such as Nullable<int> or Nullable<float> etc.


That's why IsNull should be a static method taking a parameter. string.IsNullOrEmpty is a good example. After that, nothing prevents you from making it an extension method.

public class MyClass
{
    public static bool IsNull(MyClass other)
    { return ReferenceEquals(other, null); }
    public static bool HasValue(MyClass other)
    { return !IsNull(other); }
    // other code
}

public static class MyClassExtension
{
   public static bool IsNull(this MyClass myClass)
   {
       return MyClass.IsNull(myClass);
   }
}

This will let you do the following without throwing:

MyClass myClass = null;
if(myClass.IsNull())
{
   //...
}


If you're not initializing x so it is null, you need to do this

 myClass x = new myClass();
 if (x != null) {/* is true */}


You can always use:

ReferenceEquals(x, null)

this returns a boolean value showing whether x is null.


  1. You should not do this at all.
  2. You should only do this for an immutable class, if you really must. And then,
  3. You should follow the guidelines


public class myClass: IEquatable<myClass>
{
    public static bool operator ==(myClass cA, myClass  cB)
    { return (cB == null && cA = null) || (cA is myClass && cB is myClass && cB.Equals(cA)); }
} 


Why not just override GetHashCode and Equals?

when you override them both it allows you do do both == and != very easily..

0

精彩评论

暂无评论...
验证码 换一张
取 消