Two dimensional arrays not IEnumerable?

I have a function that should accept two types of data - the MyClass Observable collection and the myclass two-dimensional array. The structure does not matter, because I use the linq query in this collection. For example - give me all the elements in the collection, where Myclass.Value == 3.

So, I created a function that takes a parameter of type IEnumerable<MyClass> , since both two-dimensional arrays and observable collections implement IEnumerable, and it should work.

However, when I try to pass the parameter myClass [,] to my function, a compile-time error occurs - The argument type Myclass [,] cannot be assigned to the parameter type IEnumerable<MyClass> .

What's wrong? thanks.

+4
source share
1 answer

Rectangular arrays implement IEnumerable , but they do not implement IEnumerable<T> (for any T , including the obvious element type). You can easily fix this by calling Cast :

 SomeMethod(array.Cast<MyClass>()); 
+9
source

Source: https://habr.com/ru/post/1389493/


All Articles