I have the following class that doesn’t implement IEnumerable
but is working perfectly with foreach
. And also, arrays are working without implementing IEnumerable
.
So why does it keep saying that IEnumerable
needs to be implemented in collections to use with foreach
. I’m confused, please assist me.
class Parent { public int MyProperty { get; set; } public void GetData() { Console.WriteLine("parent"); } } Parent[] p = new Parent[3] { new Parent(){MyProperty=90}, new Parent(){MyProperty=50}, new Parent(){MyProperty=100} }; foreach (var item in p) { Parent it = item; Console.WriteLine(it.MyProperty); }