久久久久久久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 使值類型可以為空?)
主站蜘蛛池模板: 成人黄性视频 | 中文字幕第一区综合 | 青青艹在线视频 | 欧美成人久久 | 日本特黄特色aaa大片免费 | 激情网站在线观看 | 精品黄色片 | 中文字幕观看 | 91视频精品| 成人a毛片 | 欧美日韩一二三区 | 中文在线字幕免费观 | 欧美成人精品一区二区三区在线看 | 日韩特黄| 日韩欧美综合 | 中文字幕在线不卡 | 中文字幕久久久久 | av国产精品| 久草福利在线视频 | 艳妇臀荡乳欲伦交换h漫 | 成人精品免费 | 日本一区二区三区四区五区 | 亚洲亚洲人成综合网络 | 日韩专区在线观看 | 欧美激情久久久 | 亚洲一区 | 日韩精品视频一区二区三区 | av在线免费网站 | 久久精品久久久久 | 91亚色视频 | 在线观看亚洲 | 97精品视频在线观看 | 精久久| 久久不射网 | 黄色片中文字幕 | 一区二区三区国产视频 | 黄色小视频免费观看 | 国内精品一区二区三区 | 清清草视频 | 91亚洲精品在线 | 国产精品久久久久久无人区 |