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

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

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

問題描述

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

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

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;
}

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

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 方法的開發人員覆蓋了 == 和 != 運算符.

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);
}

很好很花哨,對吧?

但是,當 Person 對象為 null 時會發生什么?

However, what happens when a Person object is null?

你不能寫:

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

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

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

person.Equals()

方法調用,因為您不能在空實例上調用方法.

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

另一方面,您無法在 == 覆蓋中明確檢查此條件,因為它會導致無限遞歸(和堆棧溢出 [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);
}

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

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);
}

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

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

相關文檔推薦

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 使值類型可以為空?)
主站蜘蛛池模板: 欧美一级做性受免费大片免费 | 午夜激情网| 亚洲视频一区二区三区 | 九九在线 | 欧美日韩中文字幕在线播放 | 午夜视频免费在线 | 四虎永久影院 | 亚洲精品视频播放 | 色.com| 国产精品午夜电影 | 成人h动漫精品一区二区器材 | 中文字幕高清一区 | 久久国产美女视频 | 婷婷99 | 国产区精品在线观看 | 日本精品视频一区二区三区四区 | 国产亚洲精品91 | 成人激情免费视频 | 国产一区二区三区色淫影院 | 亚洲一区二区中文字幕 | 超碰免费在线 | 91亚洲视频在线 | 国产91一区| 成人免费在线电影 | 国产精品欧美一区二区 | 欧美日韩精品免费观看 | 国产一区二区三区在线视频 | 欧美视频免费在线观看 | 黄色网址免费在线观看 | 9191av| 亚洲a人 | 欧美综合一区二区 | 亚洲精品高清视频在线观看 | 久久一区二区视频 | 国产精品久久久久久久岛一牛影视 | 国产精品久久9 | 久久男人天堂 | 天天综合永久 | 国产91久久精品一区二区 | 日本成年免费网站 | 亚洲精品大全 |