hon*_*pei 1 c# linq ienumerable deferred-execution
在下面的代码中,我理解第二个初始化打印一个"外部"和三个"内部".但是为什么第一个根本不打印,我希望它打印一个"外面".
DeferExecution a = new DeferExecution(); // prints nothing
DeferExecution b = new DeferExecution(null); // print one "outside" and three "inside".
class DeferExecution
{
public IEnumerable<string> Input;
public DeferExecution()
{
Input = GetIEnumerable();
}
public DeferExecution(string intput)
{
Input = GetIEnumerable().ToArray();
}
public IEnumerable<string> GetIEnumerable()
{
Console.WriteLine("outside");
var strings = new string[] {"a", "b", "c"};
foreach (var s in strings)
{
Console.WriteLine("inside");
yield return s;
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2735 次 |
| 最近记录: |