Not that it would be better, but I’m trying to get my head around turning the following method syntax to query syntax to see the difference.
long diskSpace = Directory.EnumerateDirectories(@"c:\")
.SelectMany(Directory.EnumerateFiles)
.Sum(fileSize => new FileInfo(fileSize).Length);
That query is mostly equivalent to:
(I’ve renamed
fileSizetofileto more accurately represent the meaning, btw.)There’s one actual difference in this case – we’re creating a new delegate which calls
Directory.EnumerateFilesrather than directly creating a delegate from theDirectory.EnumerateFilesmethod group. In other words, it’s one extra level of redirection – but this won’t have any effect on the results and I’d be amazed if it had any significant performance impact.