gpt4 book ai didi

c# - 为什么 Visual Studio 2010 中未到达断点?

转载 作者:行者123 更新时间:2023-12-02 00:37:33 25 4
gpt4 key购买 nike

    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时,黄色箭头仅覆盖主功能 block ,调试终止。它根本没有达到“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.

关于c# - 为什么 Visual Studio 2010 中未到达断点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28271001/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com