I see two problems in the code. Both of them come from not understanding what the FileInfo
struct is for. I'll fix one of the errors, and leave you to learn from my fix to do the second yourself.
private static int CalculateLongWordsCount(FileInfo file, int minWordLength)
{
return File.ReadLines(file.FullName).
Select(line => line.Split(' ').Count(word => word.Length > minWordLength)).
Sum();
}
You could also use File.ReadAllText()
for this and it would be simpler and more closely resemble the original code, but I prefer ReadLines()
for giving at least some protection against large files blowing up your memory.
The other problem is in this expression:
firstone.IndexOf(file)
But, again, I leave this to you to fix, so that you will be sure to understand how to use the FileInfo
struct. Separate from the FileInfo
problem, this expression will also always return -1
, because no data is ever added to the firstone
list.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…