C# NullChecking:a is null vs object.Equals(a, null) [重复]

Posted

技术标签:

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

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

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

// second one
a is null;

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

【问题讨论】:

【参考方案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
        
    

【讨论】:

以上是关于C# NullChecking:a is null vs object.Equals(a, null) [重复]的主要内容,如果未能解决你的问题,请参考以下文章

C# 如何检查空值。 (值为空)或(空 == 值)。我们可以使用`is`运算符而不是==运算符[重复]

通过动态创建 linq 查询,在 c# 中为 Sql Equivalent “column is null”创建 Linq 表达式

为啥 is 运算符在给定 null 时返回 false?

NET基础:is 和 as 操作符

IF(a.testcol IS NULL,'',a.testcol) IS NOT NULL 不起作用

C# 中var as is 泛型集合