为什么String.Equals(obj对象)检查此== NULL看? [英] Why does String.Equals(Object obj) check to see if this == null?

查看:838
本文介绍了为什么String.Equals(obj对象)检查此== NULL看?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  

可能重复:
  为什么要检查这个!= NULL?

  //确定是否两个字符串匹配。
[ReliabilityContract(Consistency.WillNotCorruptState,Cer.MayFail)
公众覆盖布尔等于(obj对象)
{
    //这是要警惕反pinvokes和
    //其他主叫方不使用callvirt指令谁
    如果(这种== NULL)
        抛出新的NullReferenceException();

    字符串str = OBJ为String;
    如果(STR == NULL)
        返回false;

    如果(Object.ReferenceEquals(这一点,OBJ))
        返回true;

    返回EqualsHelper(这一点,STR);
}
 


我不明白的部分是事实,它正在检查当前情况下,,对空。注释是有点混乱,所以我想知道这是什么意见实际上意味着什么呢?

谁能给如何,这可能打破,如果说检查是不是有一个例子,这是否意味着我也应该把该检查在我的班?

解决方案

这是检查是否有作为对本地code的保护,可能会调用该函数与空指针。这不可能发生在C#中,所以你不必把类似的警卫在code。这很可能是字符串类编写C#定稿前,笔者可能会认为它很重要,以防止空,也许这只是常见的叫字符串这可以很容易地调用方法对空从本地code和其他地方的方法。

请注意,即使你设法得到调用空和你没有门卫,所有将发生的是,除了会稍微不同。这可能是一个不同的异常,它可能会被不同的成员抛出,但除此之外,它是不太可能有所作为。

在换句话说,如果空检查是不存在的 EqualsHelper (或它被调用者的)会抛出异常,而不是等于。因为它是需要隐藏用户可见的函数的内部,是有意义的把支票就在年初。

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


The part I don't understand is the fact that it is checking for the current instance, this, against null. The comment is a bit confusing, so I was wondering what does that comment actually mean?

Can anyone give an example of how this could break if that check was not there, and does this mean that I should also place that check in my classes?

解决方案

That check is there as a guard against native code that may invoke the function with a null this pointer. This can't happen in C#, so you don't have to put similar guards in your code. It's quite possible that the String class was written before C# was finalized and the author might have thought it important to guard against nulls, or maybe it's just common to call String methods from native code and other places that make it easy to call methods on null.

Note that even if you do manage to get called with a null this and you don't have the guard, all that will happen is that the exception will be slightly different. It might be a different exception and it might get thrown by a different member, but otherwise it's unlikely to make a difference.

In other words, if the null check wasn't there, the EqualsHelper (or one of its callees) would throw the exception rather than Equals. Since it's desirable to hide the internals of the user-visible function, it makes sense to put the check right at the beginning.

这篇关于为什么String.Equals(obj对象)检查此== NULL看?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆