gpt4 book ai didi

c# - 为什么 String.Equals(Object obj) 检查 this == null?

转载 作者:太空狗 更新时间:2023-10-29 21:23:53 26 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Why check this != null?

// Determines whether two strings match. 
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
public override bool Equals(Object obj)
{
//this is necessary to guard against reverse-pinvokes and
//other callers who do not use the callvirt instruction
if (this == null)
throw new NullReferenceException();

String str = obj as String;
if (str == null)
return false;

if (Object.ReferenceEquals(this, obj))
return true;

return EqualsHelper(this, str);
}

我不明白的部分是它正在检查当前实例 this 是否为 null。该评论有点令人困惑,所以我想知道该评论的实际含义是什么?

任何人都可以举个例子说明如果不存在该检查会如何中断,这是否意味着我也应该在我的类(class)中放置该检查?

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com