简体   繁体   中英

Check if object is null without using ==

In my program, I have objects derived from Dictionary. I need to check if 2 objects are equal, so I made an overload operator ==.

But at a later point, I need to check if a object is null.

If (object == null)  
{...}

So at this point, the program goes into the overload operation I have defined, but will throw a NullReferenceException, since one of the objects to compare is null.

So within the overload operation, I need to check if one object is null, but without using ==, since that would give me a StackOverflowException.

How can I check this?

In my program, I have objects derived from Dictionary.

I would strongly reconsider deriving from Dictionary in the first place. That's very rarely a good idea. Favour composition over inheritance in general, and think very carefully about how you want equality (and hash codes) to behave in the face of mutable data. It's usually a bad sign if two objects are equal at one point and then not equal later.

So at this point, the program goes into the overload operation I have defined, but will throw a NullReferenceException, since one of the objects to compare is null.

That's the problem. Your overloaded == operator should not throw an exception - it should compare the value with null .

Typically an overload of == looks something like this:

public static bool ==(Foo left, Foo right)
{
    if (ReferenceEquals(left, right))
    {
        return true;
    }
    if (ReferenceEquals(left, null))
    {
        return false;
    }
    // Equals should be overridden, and should be handling the case where
    // right is null (by checking with ReferenceEquals too). Also consider
    // implementing IEquatable<Foo>
    return left.Equals(right);
}

Here Object.ReferenceEquals is used to perform the reference identity comparison. You could use this outside (in the code that you're currently considering changing) but it would be cleaner just to make == behave correctly.

You either need to cast to object,

(object)obj == null

or use object.ReferenceEquals(obj, null); (object is not a nice name for an object)

You have to check for null in your implementation of overriden ==, following these guidelines for example : http://msdn.microsoft.com/en-us/library/ms173147%28v=vs.80%29.aspx

You could also use

object.ReferenceEquals(myInstance, null);

Please note that overriding base operators such as == and/or directly overriding .Net base types such as Dictionary are both not very good practices.

overriding the == operator, as you have discovered, has non-obvious side effects and generally should be avoided.

You might consider having your object implement IComparable instead, and then calling CompareTo instead of ==.

使用 c# 7 中的 is null 运算符

if(object is null)...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM