Array.IndexOf:等于&相比于 [英] Array.IndexOf: Equals & CompareTo

查看:37
本文介绍了Array.IndexOf:等于&相比于的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MSDN文章中,它表示

在.NET Framework 2.0版中,此方法使用数组的 Equals和CompareTo方法来确定value参数指定的对象是否存在.在.NET Framework的早期版本中,使用值 Object 本身的Equals和CompareTo方法进行此确定.

In the .NET Framework version 2.0, this method uses the Equals and CompareTo methods of the Array to determine whether the Object specified by the value parameter exists. In the earlier versions of the .NET Framework, this determination was made by using the Equals and CompareTo methods of the value Object itself.

这到底是什么意思?根据我使用Reflector的了解,Array.IndexOf仍然使用对象的equals方法来确定对象在数组中的索引:

What exactly does this mean? From what I can tell using Reflector, Array.IndexOf still uses the equals method of the object to determine the index of the object in the array:

for (int j = startIndex; j < num3; j++)
{
    object obj2 = objArray[j];
    if ((obj2 != null) && obj2.Equals(value))
    {
        return j;
    }
}

这是我所期望的 ,但是我对 MSDN备注感到有些困惑.

This is what I expected, but I'm a little confused by the MSDN Remark.

推荐答案

(非常)措辞不佳,但这意味着在1.1中,它使用

搜索了 arrayElement

It's (very) poorly worded, but it means that in 1.1, it searched for an arrayElement with

value.Equals(arrayElement) == true

在2.0中,它使用来搜索

while in 2.0 it searches for one with

arrayElement.Equals(value) == true

也就是说,从1.1开始,等效的反射代码是

That is, the equivalent piece of reflected code from 1.1 was

for (int j = startIndex; j < num3; j++)
{
    object obj2 = objArray[j];
    if ((obj2 != null) && value.Equals(obj2))
    {
        return j;
    }
}

这篇关于Array.IndexOf:等于&amp;相比于的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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