I have code which accepts a parameter on which it needs to access a Length
property as well as be indexable via [index]
. When I run the following code, the last line which calls the method passing an array, fails compilation with:
Cannot convert type 'int[]' to 'IArrayLike<int>'
What should my interface definition be to accept arrays as well as other classes which provide a Length
property and are indexable.
interface IArrayLike<T> {
T this[int index] { get; set; }
int Length { get; }
}
class SomeClass<T> where T : IComparable<T> {
public static int SomeMethod(T item, IArrayLike<T> data) {
// code
}
}
int[] someArray = new int[5];
SomeClass.SomeMethod(123, someArray);