Directory.EnumerateFiles 与 Directory.GetFiles 有什么区别? [英] What is the difference between Directory.EnumerateFiles vs Directory.GetFiles?

查看:16
本文介绍了Directory.EnumerateFiles 与 Directory.GetFiles 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Directory.EnumerateFilesGetFiles 有什么区别?

显然,一个返回一个数组,另一个返回 Enumerable.

Obviously one returns an array and the other return Enumerable.

还有什么吗?

推荐答案

来自 文档:

EnumerateFiles 和 GetFiles 方法的区别如下: 当您使用 EnumerateFiles 时,您可以在返回整个集合之前开始枚举名称集合;使用 GetFiles 时,必须等待整个名称数组返回,然后才能访问该数组.因此,当您处理多个文件和目录时,EnumerateFiles 可以更高效.

The EnumerateFiles and GetFiles methods differ as follows: When you use EnumerateFiles, you can start enumerating the collection of names before the whole collection is returned; when you use GetFiles, you must wait for the whole array of names to be returned before you can access the array. Therefore, when you are working with many files and directories, EnumerateFiles can be more efficient.

所以基本上,EnumerateFiles 返回一个 IEnumerable ,它可以在某种程度上进行惰性求值,而 GetFiles 返回一个 string[] 必须完全填充才能返回.

So basically, EnumerateFiles returns an IEnumerable which can be lazily evaluated somewhat, whereas GetFiles returns a string[] which has to be fully populated before it can return.

这篇关于Directory.EnumerateFiles 与 Directory.GetFiles 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆