我想知道以下代码是否为惰性求值或者在我处理可能的
ReadAllLines()
异常的方式中会崩溃。我知道使用Where
子句时它是惰性求值的,但我不确定当我将其与ReadAllLines()
一起使用时是否仍然如此。关于如何以及为什么可能的解释将不胜感激。
var fileLines = File.ReadAllLines(filePath).Where(line =>
{
line = line.Trim();
return line.Contains("hello");
});
string search;
try
{
search = fileLines.Single();
}
catch (Exception exception)
{
...log the exception...
}
提前致谢。
T[]
永远不会是惰性的,因为它需要一个连续的内存块。 - Caramiriel