testing if an object is null
How can I test to see if an object is null.
Everything I've found says I should use:
Code:
if (myObject == null)
Unfortunately I've overloaded the == operator so this won't work. Is there another way to do this or something I could do to my overloaded operator to handle this situation?
Re: testing if an object is null
Why wouldn't that work? If your overload cannot handle a comparison to null then you need to fix it. I assume that you overrode .Equals() as well, right?
Re: testing if an object is null
Then your overloaded operator should do a nullcheck before it does the usual equality check.
Alternatively if you cast to 'object' first, you'll use the regular (non-overloaded) comparison:
Code:
if ((object)myObject == null) {
Console.WriteLine ("I'm using the regular '==' and i'm null");
}
EDIT: Generally speaking you should only override == for immutable types. So if your type is mutable, then don't override '=='. If you want value comparison, you should be overriding the 'Equals' method.
Re: testing if an object is null
Quote:
Originally Posted by
Mutant_Fruit
EDIT: Generally speaking you should only override == for immutable types. So if your type is mutable, then don't override '=='. If you want value comparison, you should be overriding the 'Equals' method.
+1 rep if I could.
Re: testing if an object is null
Unfortunately this is a school project so the choice to use .Equals() as opposed to == is not my choice to make. I think the cast will work. I hadn't thought of that. Thanks.
Re: testing if an object is null
Quote:
Originally Posted by
afireohno
Unfortunately this is a school project so the choice to use .Equals() as opposed to == is not my choice to make. I think the cast will work. I hadn't thought of that. Thanks.
You should let your teacher know that they are not really teaching this concept in the best way.
Re: testing if an object is null
Sometimes I think they give assignments with ridiculous specifications just to confuse and weed people out of the program.
Re: testing if an object is null
Quote:
Originally Posted by
afireohno
Sometimes I think they give assignments with ridiculous specifications just to confuse and weed people out of the program.
Agreed :wave:
Instructors tend to make you do things in awkward ways, perhaps its because they dont fully understand the concepts. Or because thats the only way they know how and they can grade successfully. My instructor for VB.NET (sorry... but ick) makes us write comments on all of out methods and properties like so:
Code:
' ====================================
' === MyMethod(ByVal myNameString As String)
' === myNameString: The users name.
' === Purpose: Does something with a name.
' ====================================
Personally I think its gaudy, so I came to him one day and told him about auto-generated XML Summary tags and he liked it and changed his curriculum a bit. I told him Ive been using C# for about 3 - 4 years now and Ive discovered alot of tricks that will make everyones life a little easier, lol.
Ok, Im done hijacking this thread ;)
Re: testing if an object is null
Use static Object.ReferenceEquals() method:
Code:
if (Object.ReferenceEquals(myObject, null))