English 中文(简体)
Result of calls IEquatable <T>.Equals(T obj) when this = contra and obj == no?
原标题:Result of calling IEquatable<T>.Equals(T obj) when this == null and obj == null?

http://code>IEquatable<T>Equals(T obj) do when this = = 无obj = 无?

1) This code is generated by F# compiler when implementing IEquatable<T>. You can see that it returns true when both objects are null:

    public sealed override bool Equals(T obj)
    {
        if (this == null)
        {
            return obj == null;
        }
        if (obj == null)
        {
            return false;
        }

        // Code when both this and obj are not null.
    }

2) Similar code can be found in the question "in IEquatable implementation is reference check necessary" or in the question "Is there a complete IEquatable implementation reference?". This code returns false when both objects are null.

    public sealed override bool Equals(T obj)
    {
        if (obj == null)
        {
            return false;
        }

        // Code when obj is not null.
    }

<>3> 最后一个选择是说,在<代码>this=0时,该方法的行为没有界定。

最佳回答
问题回答

由于F#的原因,(我怀疑)将空名单优化为<代码>null。

通过添加这一检查,允许在无问题的情况下在<代码>null上采用一种检查方法。

See my blog post from a while re.

In C#, this is irrelevant.

www.un.org/Depts/DGACM/index_spanish.htm 回答问题:

它应当退回<代码>true,因为这两种情况均为null,并视为平等。

如果this是无效的,则可以不要求使用该代码,这样就不必考虑(在C#中,有些语文允许一个无效的物体使用某种方法,尽管显然如果在内部审查任何不存在的领域,就会有错误。 考虑:

return x.Equals(y);

If x is null, we don t even get to call into Equals for the null check to count.

因此,我们只需要考虑:

public bool Equals(T obj)
{
  if(obj == null)
    return false;
  //logic defining equality here.
}

如果这两种物体都有可能失效,那么,我们是在从一个静态的<条码> =上审查这些物体时,操作者必须放弃或从<条码>上审查。 I. 平等 比较和比较;T> 执行:

public bool Equals(T x, T y)
{
  if(x == null)
    return y == null;
  if(y == null)
    return false;
  //logic defining equality here.
}

指出,如果平等可能很长一段时间才能确定(例如比较长处),那么我们就可以利用身份要求平等这一事实——这是永远平等的,即使Ayn Rand可能表明这一点; 还有一种算法将某一项内容与它本身进行比较,使这一捷径的价值包括在内。 在这种情况下,身份比较已经包括检查两者都无效,因此,我们不再这样做:

public bool Equals(T x, T y)
{
  if(ReferenceEquals(x, y))
    return true;
  if(x == null || y == null)
    return false;
  //logic defining equality here.
}

就大多数方法而言,我假定不明确的行为,用this=null。 这是因为大多数方案者都根据以下假设撰写了他们的代码:this!=null,如果在C#中写上呼吁书,C#规格就保证了这一点。

因此,每个打字机<代码>x。 平等(y),或者应当知道<代码>x不是null,或添加一个人工<编码>null<>/code>。

在大多数情况下,我只字不提<条码>。 EqualityComparer<T>.Default 。

I would definitelly go with option 1:

    if (this == null)
    {
        return obj == null;
    }
    if (obj == null)
    {
        return false;
    }

null object always equals null object.

如果这一=年金,你将获得一个称为“平等”的暂时例外。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签