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

标签 c# visual-studio-2010 visual-studio-debugging

    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/

相关文章:

c# - 为什么我的 MVC 代码不抛出验证错误?

SSIS 脚本任务目录中使用的 c# FileInfo 类。GetFiles SearchOption.TopDirectoryOnly 被忽略

c++ - 进程外渲染

node.js - ( Node :11684) [DEP0062] DeprecationWarning: `node --inspect --debug-brk` is deprecated. 请改用 `node --inspect-brk`

windows - 将变量值打印到控制台 visual c++ windows store Modern UI app

c# - 使用 Entity Framework 保存实体列表

c# - 匹配整个单词或短语的正则表达式

visual-studio-2010 - Visual Studio 2010卡住了

c# - asp 默认设计样式仅出现在设计时,在运行时 visual studio 2012 中消失

c# - 在 VS 中检查时,有什么方法可以使对象的属性以特定顺序出现?