2

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 immutable and nullable class, exactly like an immutable, nullable struct behaves (such as Nullable<int> or Nullable<float> etc.

2
  • Just thinking aloud here, but what would be the disadvantage of always returning true in your overload when cA or cB is null? Commented Feb 25, 2011 at 14:45
  • The is operator calls in public static bool operator ==(myClass cA, myClass cB) { return cA is myClass && cB is myClass && cB.Equals(cA); } are not needed. Commented Feb 25, 2011 at 14:46

6 Answers 6

5

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()) { //... } 
Sign up to request clarification or add additional context in comments.

2 Comments

Combine with @jjrdk's ReferenceEquals suggestion from below, I think this will work.. Thanks!
Oh! Thank you. I was hesitant in doing it myself so I +1 him instead. :) Thanks for the accept.
3

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

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

4 Comments

Whether or not x is null, writing (x != null) calls the code in my operator override....
added more of the code to make this explicit... but the != is NOT the standard .net !=.... It is running my override of !=. So whether the variable is or is not null has nothing to do with it. It is the code in my override that controls what the != method will return.
yes but the is in your == comparisons fails because the second argument is null, I don't see why you expect a null check to return false when the object is null
yes, this was exactly the problem... How to check for null when every call to == or to != was being rerouted to my overrides...
3

You can always use:

ReferenceEquals(x, null) 

this returns a boolean value showing whether x is null.

1 Comment

Wish I could check two answers as solutions, your answer, combined with Coincoins from below ended up being the solution... Thanks
2
  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

1 Comment

1) Thanks for your help. 2) I need to do this. 3) This type is immutable. 4) Stay in your box with your guidelines.
2
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)); } } 

Comments

1

Why not just override GetHashCode and Equals?

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.