Directory.EnumerateFiles VS Directory.GetFiles之间的区别是什么?区别、EnumerateFiles、Directory、GetFiles

2023-09-02 10:21:56 作者:与你颠沛流离。

VS 的GetFiles

显然,其中一个返回数组和其他返回枚举。

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.