Mark Good
Mark Good

Reputation: 4303

Array.IndexOf: Equals & CompareTo

In this MSDN article, it says that

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.

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

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

Upvotes: 1

Views: 1451

Answers (1)

stevemegson
stevemegson

Reputation: 12093

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

value.Equals(arrayElement) == true

while in 2.0 it searches for one with

arrayElement.Equals(value) == true

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

Upvotes: 3

Related Questions