久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

覆蓋 == 運算符.如何與空值進行比較?

Overriding == operator. How to compare to null?(覆蓋 == 運算符.如何與空值進行比較?)
本文介紹了覆蓋 == 運算符.如何與空值進行比較?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

可能的重復(fù):
如何檢查空值在沒有無限遞歸的=="運算符重載中?

對此可能有一個簡單的答案……但我似乎無法回答.這是一個簡化的例子:

There is probably an easy answer to this...but it seems to be eluding me. Here is a simplified example:

public class Person
{
   public string SocialSecurityNumber;
   public string FirstName;
   public string LastName;
}

假設(shè)對于這個特定的應(yīng)用程序,如果社會安全號碼匹配,并且兩個名字匹配,那么我們指的是同一個人".

Let's say that for this particular application, it is valid to say that if the social security numbers match, and both names match, then we are referring to the same "person".

public override bool Equals(object Obj)
{
    Person other = (Person)Obj;
    return (this.SocialSecurityNumber == other.SocialSecurityNumber &&
        this.FirstName == other.FirstName &&
        this.LastName == other.LastName);
}

為了保持一致,我們也為團隊中不使用 .Equals 方法的開發(fā)人員覆蓋了 == 和 != 運算符.

To keep things consistent, we override the == and != operators, too, for the developers on the team who don't use the .Equals method.

public static bool operator !=(Person person1, Person person2)
{
    return ! person1.Equals(person2);
}

public static bool operator ==(Person person1, Person person2)
{
    return person1.Equals(person2);
}

很好很花哨,對吧?

但是,當(dāng) Person 對象為 null 時會發(fā)生什么?

However, what happens when a Person object is null?

你不能寫:

if (person == null)
{
    //fail!
}

因為這將導(dǎo)致 == 運算符覆蓋運行,并且代碼將失敗:

Since this will cause the == operator override to run, and the code will fail on the:

person.Equals()

方法調(diào)用,因為您不能在空實例上調(diào)用方法.

method call, since you can't call a method on a null instance.

另一方面,您無法在 == 覆蓋中明確檢查此條件,因為它會導(dǎo)致無限遞歸(和堆棧溢出 [dot com])

On the other hand, you can't explicitly check for this condition inside the == override, since it would cause an infinite recursion (and a Stack Overflow [dot com])

public static bool operator ==(Person person1, Person person2)
{
    if (person1 == null)
    {
         //any code here never gets executed!  We first die a slow painful death.
    }
    return person1.Equals(person2);
}

那么,您如何覆蓋 == 和 != 運算符以實現(xiàn)值相等并仍然考慮空對象?

So, how do you override the == and != operators for value equality and still account for null objects?

我希望答案不是那么簡單.:-)

I hope that the answer is not painfully simple. :-)

推薦答案

使用 object.ReferenceEquals(person1, null) 或新的 is 運算符 而不是 == 運算符:

Use object.ReferenceEquals(person1, null) or the new is operator instead of the == operator:

public static bool operator ==(Person person1, Person person2)
{
    if (person1 is null)
    {
         return person2 is null;
    }

    return person1.Equals(person2);
}

這篇關(guān)于覆蓋 == 運算符.如何與空值進行比較?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to check if String is null(如何檢查字符串是否為空)
Equals(item, null) or item == null(Equals(item, null) 或 item == null)
What does the question mark in member access mean in C#?(成員訪問中的問號在 C# 中是什么意思?)
The || (or) Operator in Linq with C#(||(或)C# 中的 Linq 運算符)
C# null coalescing operator equivalent for c++(C# 空合并運算符等效于 C++)
How to make a value type nullable with .NET XmlSerializer?(如何使用 .NET XmlSerializer 使值類型可以為空?)
主站蜘蛛池模板: 女人av| 中文成人在线 | 夜夜摸夜夜操 | 81精品国产乱码久久久久久 | 久久久久久国产精品 | 91久久久www播放日本观看 | 欧美日韩一区在线 | 福利网站导航 | 国产午夜精品视频 | 免费av观看 | 欧美最猛黑人xxxx黑人 | 久久久xxx | 久久久久久国产 | 成人 在线 | 国产精品美女www爽爽爽视频 | 国产三级网站 | 91精品国产综合久久久久 | 欧美性受 | 成人精品一区二区 | 综合久久综合久久 | 视频一二区 | 日韩成人中文字幕 | 中文字幕综合 | 国产久 | 国产一级片一区二区三区 | 91在线电影| 91美女在线| 91精品国产综合久久精品 | 日韩精品一区二区三区中文字幕 | 在线视频中文字幕 | 91在线中文字幕 | 久久国产亚洲 | 操人网 | 综合伊人| 日本成人免费网站 | 久久久精品一区二区 | 欧美二区三区 | 日韩精品中文字幕一区二区三区 | 久久久999免费视频 999久久久久久久久6666 | 日本不卡在线视频 | 91精品国产欧美一区二区成人 |