最近我遇到了这样的问题:
What numbers will be printed considering the following code:
class Program
{
static void Main(string[] args)
{
int[] numbers = { 1, 3, 5, 7, 9 };
int threshold = 6;
var query = from value in numbers where value >= threshold select value;
threshold = 3;
var result = query.ToList();
result.ForEach(Console.WriteLine);
Console.ReadLine();
}
}
Run Code Online (Sandbox Code Playgroud)
回答: 3, 5, 7, 9
这让我很惊讶.我认为该threshold值将在查询构造中放入堆栈,稍后在执行时,该数字将被拉回并在条件中使用..这种情况没有发生.
另一种情况(在执行之前numbers设置null):
static void Main(string[] args)
{
int[] numbers = { 1, 3, …Run Code Online (Sandbox Code Playgroud)