【问题标题】:C# NullChecking: a is null vs object.Equals(a, null) [duplicate]C# NullChecking:a is null vs object.Equals(a, null) [重复]
【发布时间】:2020-07-11 22:34:38
【问题描述】:

当前版本的 C#(高于 7.1)有以下两种方法来检查变量是否为空:

// first one
object.Equals(a, null);

// second one
a is null;

我想知道,每种方式的优点是什么。有什么特殊情况可以同时使用吗?

【问题讨论】:

    标签: c# .net isnull


    【解决方案1】:

    Equals 证明了同一性,这意味着对象完全相同。您可以覆盖 Equals,在这种情况下您可以检查它们的属性。 (如果是 object.Equals,则证明身份)。


    如果 expr 不为 null,并且以下任何一项为真,则 is 表达式为真:(SRC: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/is)

    expr 是与 type 相同类型的实例。

    expr is an instance of a type that derives from type. In other words, the result of expr can be upcast to an instance of type.
    
    expr has a compile-time type that is a base class of type, and expr has a runtime type that is type or is derived from type. The compile-time type of a variable is the variable's type as defined in its declaration. The runtime type of a variable is the type of the instance that is assigned to that variable.
    
    expr is an instance of a type that implements the type interface.
    

    您可以在此处找到如何覆盖 Equals: https://docs.microsoft.com/de-de/dotnet/api/system.object.equals?view=netframework-4.8

    这里有一些例子:


        public class Word
    {
        public string Name;
    
        public Word(string name)
        {
            Name = name;
        }
    
        //Euqals is no overriden
        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }
    
    
        public class OtherClass
        {
            public OtherClass()
            {
                Word word = new Word("word");
                Word otherWord = new Word("word");
                string a = null;
    
                var r = a == null; //true
                var r2 = object.Equals(a, null);//true      
                var r3 = word.Name is string; //true
                var r4 = word.Name is Word; //false
                var r5 = word.Equals(otherWord); //false
            }
        }
    

    如果你在你的类中覆盖 equals:


    public class Word
    {
        public string Name;
    
        public Word(string name)
        {
            Name = name;
        }
    
        //Override Equals
        public override bool Equals(object obj)
        {
            if ((obj == null) || !this.GetType().Equals(obj.GetType()))
            {
                return false;
            }
            else
            {
                Word thisWord = (Word) obj;
                return (Name == thisWord.Name) /* && (Other properties)*/;
            }
        }
    
    
        public class OtherClass
        {
            public OtherClass()
            {
                Word word = new Word("word");
                Word otherWord = new Word("word");
                string a = null;
    
                var r = a == null; //true
                var r2 = object.Equals(a, null);//true      
                var r3 = word.Equals(otherWord); //true
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2013-07-20
      • 2015-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-07
      • 2011-12-24
      • 1970-01-01
      • 2018-02-28
      相关资源
      最近更新 更多