【发布时间】:2018-03-05 16:07:56
【问题描述】:
我想保存关于以后如何确定值的“说明”,而不是保存当前时间的实际值。 这甚至可能吗?
一个简单的 C# 示例:
int[] myArray = new int[2];
Dictionary<string, int> myDictionary = new Dictionary<string, int>();
//dictionary type can be changed if required
myArray[0] = 1;
myArray[1] = 2;
myDictionary.Add("total", (myArray[0] + myArray[1]) ); // Don't evaluate the value now
myArray[0] = 3;
myArray[1] = 4;
Console.WriteLine("total 2 = " + myDictionary["total"]); // Evaluate the value now
//Desired output: 7 (3+4), actual output = 3 (1+2)
【问题讨论】:
-
好奇怪的字典,除了
total还有别的吗? -
* Tim,这只是一个例子,在这本词典中只有“total”,但在真正的词典中会有更多的条目。 * 费尔南多,我去看看。
-
* vc 74,我不知道怎么读,它看起来像一个返回摘要的匿名函数?
-
一组按需计算结果的指令在 C# 中称为“方法”。将您的代码放入方法中。当您希望它被激活时,调用方法。
标签: c# lazy-evaluation evaluation delayed-execution