我发现这篇文章是关于Lazy:C#4.0中的懒惰 - 懒惰
使用Lazy对象获得最佳性能的最佳实践是什么?有人能指出我在实际应用中的实际用途吗?换句话说,我什么时候应该使用它?
Directory.EnumerateFilesvs 之间有什么区别GetFiles?
显然,一个返回一个数组,另一个返回Enumerable.
还要别的吗?
我已编写以下例程来手动遍历目录并在C#/ .NET中计算其大小:
protected static float CalculateFolderSize(string folder)
{
float folderSize = 0.0f;
try
{
//Checks if the path is valid or not
if (!Directory.Exists(folder))
return folderSize;
else
{
try
{
foreach (string file in Directory.GetFiles(folder))
{
if (File.Exists(file))
{
FileInfo finfo = new FileInfo(file);
folderSize += finfo.Length;
}
}
foreach (string dir in Directory.GetDirectories(folder))
folderSize += CalculateFolderSize(dir);
}
catch (NotSupportedException e)
{
Console.WriteLine("Unable to calculate folder size: {0}", e.Message);
}
}
}
catch (UnauthorizedAccessException e)
{
Console.WriteLine("Unable to calculate folder …Run Code Online (Sandbox Code Playgroud)