加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – DirectoryInfo枚举

发布时间:2020-12-16 00:21:30 所属栏目:百科 来源:网络整理
导读:在学习C#基础知识时,我已经了解到foreach适用于那些已实现IEnumerable接口的集合.到目前为止一直很好,但今天当我遇到DirectoryInfo时,我感到很困惑. 如果DirectoryInfo没有实现IEnumerable,那么foreach是如何工作的呢? DirectoryInfo[] dirListing = new Di
在学习C#基础知识时,我已经了解到foreach适用于那些已实现IEnumerable接口的集合.到目前为止一直很好,但今天当我遇到DirectoryInfo时,我感到很困惑.

如果DirectoryInfo没有实现IEnumerable,那么foreach是如何工作的呢?

DirectoryInfo[] dirListing = new DirectoryInfo(@"F:eBook").GetDirectories();

foreach (DirectoryInfo dir in dirListing)
{
    Console.WriteLine(dir.Name);
}

请告诉我…….

解决方法

您正在使用数组.

所有数组都派生自实现IEnumerable的Array抽象类.

从MSDN上的Arrays (C# Programming Guide)开始:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读