我有這樣瞭解懶評價在C#
class Context
{
public List<Student> lists;
public Context()
{
lists = new List<Student>() {
new Student { Name="foo",Standard="first",subjects=new Subjects { Geography=50,History=81,Science=70} },
new Student { Name="carl",Standard="first",subjects=new Subjects { Geography=40,History=51,Science=50} },
new Student { Name="ben",Standard="first",subjects=new Subjects { Geography=30,History=91,Science=60} },
new Student { Name="peter",Standard="first",subjects=new Subjects { Geography=80,History=71,Science=40} }
};
}
}
class Client
{
static void Main(string[] args)
{
List<Student> lists = new Context().lists;
var result = lists.Where(x => x.subjects.History > 60);
lists.Add(new Student { Name = "tan", Standard = "first", subjects = new Subjects { Geography = 40, History = 81, Science = 60 } });
lists.Add(new Student { Name = "ran", Standard = "first", subjects = new Subjects { Geography = 30, History = 70, Science = 50 } });
lists.Add(new Student { Name = "ranky", Standard = "first", subjects = new Subjects { Geography = 20, History = 31, Science = 40 } });
lists.Add(new Student { Name = "franky", Standard = "first", subjects = new Subjects { Geography = 50, History = 51, Science = 30 } });
foreach (var data in result) {
Console.WriteLine(data);
}
}
}
現在代碼在調試時,加入一些元素,當我把鼠標放在變量之前,我得到這樣
結果加入一些元素的名單後,當我將鼠標懸停在變量i得到這樣
結果 ,但根據懶惰執行的概念,當它到達foreach方法時加載數據,那麼爲什麼數據已經加載並在調試器中看到。我是否理解懶惰評估 更新1 根據以前關於我的截圖,如果點擊「結果視圖」強制加載數據,那麼,這裏是我第二個場景,我只是加載數據,可以看到形成屏幕截圖
是不是假設使用foreach進行調用時加載數據?請幫助我瞭解懶惰評估的工作原理。謝謝。
你在你的問題所引用的代碼看上去一點也不像截圖你已經展示過。請提供一個[mcve],並顯示來自*的輸出*(可以再次顯示文本 - 根本不需要截圖) –
好吧,只需一秒...感謝您的幫助 –
'Concat'不會修改數字,它會返回一個新的被修改的列表。你可能想要'result = numbers.Concat(num2);'來代替'numbers.Concat(num2);'。 – Quantic