ArgumentException thrown from Array.BinarySearch <T> (T [], int, int, T)

This MSDN page documents the method.

public static int BinarySearch<T>(
    T[] array,
    int index,
    int length,
    T value
)

In the list of exceptions indicating that an ArgumentException is thrown under the following circumstances:

index and length do not specify a valid range in the array.
-or-
has a type that is incompatible with array elements.

How is this possible? Under what circumstances T cannot be compatible with an element from T []? I suspect that this may be a mistake in the documentation, or am I missing something fundamental?

+4
source share
1 answer

, .

Fill :

public static void Fill(object[] array, int index, int count, object value)
    {
        for (int i = index; i < index + count; i++)
        {
            array[i] = value;
        }
    }

ArrayTypeMismatchException :

string[] strings = new string[100];
Fill(strings, 0, 100, "Undefined");
Fill(strings, 0, 10, null);
Fill(strings, 90, 10, 0); // a boxed 0 is not a string. 

, Fill :   public static void FillT (T [] array, int index, int count, T)       {           for (int = index; < index + count; ++)           {               array [i] = ;           }       }

,

FillT(strings, 90, 10, 0);

FillT<string>(strings, 90, 10, 0);

, , string, int . , .

0

All Articles