为什么在 Visual Studio 2010 中没有达到断点?
Why breakpoint is not reached in VisualStudio 2010?
static void Main(string[] args)
{
List<int> li = new List<int>() { 1, 20, 30, 4, 5 };
GetNosLessThan5(li);
}
public static IEnumerable<int> GetNosLessThan5(List<int> numbers)
{
foreach (var v in numbers)
{
if (v < 5)
yield return v;
}
}
我在 void main 的开头放置了一个调试点。当我连续按f11时,黄色箭头只覆盖了主要功能块,调试终止。它永远不会到达 all.Confused 处的 "getnoslessthan5" 函数。!
您实际上并没有迭代结果,因此 GetNosLessThan5
函数的实际主体永远不会执行。编译器在后台创建了一个迭代器,但需要为函数体实际枚举它 运行.
参见MSDN Documentation about Iterators。
An iterator can be used to step through collections such as lists and arrays.
An iterator method or get accessor performs a custom iteration over a collection. An iterator method uses the Yield (Visual Basic) or yield return (C#) statement to return each element one at a time. When a Yield or yield return statement is reached, the current location in code is remembered. Execution is restarted from that location the next time the iterator function is called.
You consume an iterator from client code by using a For Each…Next (Visual Basic) or foreach (C#) statement or by using a LINQ query.
static void Main(string[] args)
{
List<int> li = new List<int>() { 1, 20, 30, 4, 5 };
GetNosLessThan5(li);
}
public static IEnumerable<int> GetNosLessThan5(List<int> numbers)
{
foreach (var v in numbers)
{
if (v < 5)
yield return v;
}
}
我在 void main 的开头放置了一个调试点。当我连续按f11时,黄色箭头只覆盖了主要功能块,调试终止。它永远不会到达 all.Confused 处的 "getnoslessthan5" 函数。!
您实际上并没有迭代结果,因此 GetNosLessThan5
函数的实际主体永远不会执行。编译器在后台创建了一个迭代器,但需要为函数体实际枚举它 运行.
参见MSDN Documentation about Iterators。
An iterator can be used to step through collections such as lists and arrays.
An iterator method or get accessor performs a custom iteration over a collection. An iterator method uses the Yield (Visual Basic) or yield return (C#) statement to return each element one at a time. When a Yield or yield return statement is reached, the current location in code is remembered. Execution is restarted from that location the next time the iterator function is called.
You consume an iterator from client code by using a For Each…Next (Visual Basic) or foreach (C#) statement or by using a LINQ query.