In a different question on stackoverflow somebody suggested to write an extension method for an array, but used the this IList<T>
interface in the extension method. I commented it should be an array but he declined. I tested it, and of course, he's right... :)
Extension method:
public static void Fill<T>(this IList<T> array, T value)
{
for(var i = 0; i < array.Count; i++)
{
array[i] = value;
}
}
Test code:
[Test]
public void Stackoverflow()
{
int[] arr = new int[] { 1,2,3,4};
arr.Fill(2);
Assert.AreEqual(2, arr[0]);
Assert.AreEqual(2, arr[1]);
Assert.AreEqual(2, arr[2]);
Assert.AreEqual(2, arr[3]);
}
An array
is not an IList<T>
. Why does this even compile? Let alone, pass?!