Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DirectoryInfo Enumeration

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.......

like image 918
Ashish Kalra Avatar asked Dec 10 '25 15:12

Ashish Kalra


1 Answers

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 implements IEnumerable and IEnumerable<T>, you can use foreach iteration on all arrays in C#.

like image 60
Oded Avatar answered Dec 12 '25 05:12

Oded



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!