简体   繁体   中英

Extend “object” with a null check more readable than ReferenceEquals

I tried to extend "object" to allow a more readable check if an object is null.

Now, object.ReferenceEquals really checks for a null object, (the rare times it will not apply are since the operator == can be overridden. the object.Equals(null) method can also be overridden).

But the object.ReferenceEquals(null, obj); is not too readable is it?... So, I thought, why not write an extension method to the System.object that will provide that check using object.IsNull(obj);

I've tried:

public static class MyExtClass
{
    // the "IsNull" extension to "object"  
    public static bool IsNull(this object obj)
    {
        return object.ReferenceEquals(obj, null);
    }
}

public SomeOtherClass
{
     public static void TryUsingTheExtension()
     {
          object obj;

          // Why does this line fail? the extension method is not recognized
          // I get: 'object' does not contain a definition for "IsNull"
          bool itIsANull = object.IsNull(obj);
     }
}

What did I miss?

尝试

bool itIsANull = obj.IsNull();

Extension methods can be invoked only on instance and not on a class that they extend. So this line of code bool itIsANull = object.IsNull(obj); is incorrect because object is type and not an instance. Change it to :

bool itIsANull = (new object()).IsNull();

Or you can call it on class MyExtClass but not on object class (which is located in mscore.lib) :

MyExtClass.IsNull(new object());

PS It looks like you missed something about extension methods. The truth is that they have nothing to do with classes that they extend. It's just a convenience that is provided for us by Intellisense with use of reflection.

Object class is located in mscorelib and is immutable. You can't add something to it. But what really happens is that Intellisense searches for all public methods that are located in public static classes and accept first argument with keyword 'this' as parameter. If one is found it's 'mapped' to the class that it extends. So when we type obj.MyExtMethod() on instance of that class it is automatically converted by compiler to Helper.MyExtMethod(obj); (if helper is our static class);

You wrote an extension method, and extension methods exist in a different type but extend objects of the specified type by another method.

But when you call object.IsNull() , then you are looking for a static method that exists on the object type.

Instead, you have two ways to call your method:

// either the static method on the class
MyExtClass.IsNull(obj);

// or using the actual feature of extension methods
obj.isNull();

Because it's an extension method, the latter form will be automatically converted into the former at compile time.

You are calling the extension method on the object itself. You should call the methd on the instance instead -

bool itIsANull = obj.IsNull()

Try:

    class Program
    {
        static void Main(string[] args)
        {
            var o = new object();
            if (o.IsNull())
            {
                Console.Write("null");
            }
        }
    }

    public static class Request
    {
        public static bool IsNull(this object obj)
        {
            return ReferenceEquals(obj, null);
        }
    }
public static class MyExtClass
    {
        // the "IsNull" extension to "object"  
        public static bool IsNull(this object obj)
        {
            return object.ReferenceEquals(obj, null);
        }

    }

    public class SomeOtherClass
    {
        public static void TryUsingTheExtension()
        {
            object obj =null;

            bool itIsANull = obj.IsNull();
        }

    }

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