While learning C# basics i have learned that foreach works on those collection that have implemented IEnumerable interface. So far so good but today when I have came across DirectoryInfo I got confused.
If DirectoryInfo doesn't implement IEnumerable, then how does it come that foreach works?
DirectoryInfo[] dirListing = new DirectoryInfo(@"F:\eBook").GetDirectories();
foreach (DirectoryInfo dir in dirListing)
{
Console.WriteLine(dir.Name);
}
Please tell me.......
You are using an array.
All arrays derive from the Array abstract class that does implement IEnumerable.
From Arrays (C# Programming Guide) on MSDN:
Array types are reference types derived from the abstract base type
Array. Since this type implementsIEnumerableandIEnumerable<T>, you can use foreach iteration on all arrays in C#.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With