调试器未命中断点

发布于 2024-07-25 03:45:33 字数 666 浏览 5 评论 0原文

我发现了一些很奇怪的东西(我认为!)。 如果我尝试在 yes() 方法中放置断点,则在执行该函数时它永远不会暂停程序。 如果我尝试对任何其他代码行执行相同的操作,它将按预期工作。 这是一个错误,还是有什么东西在逃避我?

过滤器将返回 2 个对象,除了调试器之外,一切似乎都按预期工作。

private void Form1_Load(object sender, EventArgs e) {
    List<LOL> list = new List<LOL>();
    list.Add(new LOL());
    list.Add(new LOL());

    IEnumerable<LOL> filter = list.Where(
        delegate(LOL lol) {
            return lol.yes();
        }
    );

    string l = "";   <------this is hit by the debugger
}

class LOL {
    public bool yes() {
        bool ret = true; <---------this is NOT hit by the debugger
        return ret;
    }
}

I found something quite odd(I think!). If I try to put a breakpoint in the yes() method, it will never pause the program when it executes the function. If I try to do the same to any other line of code, it will work just as expected. Is it a bug, or is there something that's escaping me?

The filter will return the 2 objects, everything seems to be working as expected except the debugger.

private void Form1_Load(object sender, EventArgs e) {
    List<LOL> list = new List<LOL>();
    list.Add(new LOL());
    list.Add(new LOL());

    IEnumerable<LOL> filter = list.Where(
        delegate(LOL lol) {
            return lol.yes();
        }
    );

    string l = "";   <------this is hit by the debugger
}

class LOL {
    public bool yes() {
        bool ret = true; <---------this is NOT hit by the debugger
        return ret;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

ゃ人海孤独症 2024-08-01 03:45:34

Enumerable.Where 是一个惰性运算符 - 除非您调用通过 where 返回的 IEnumerable 的内容(即对其调用 .ToList() ),否则您的函数将不会被调用。

尝试将您的代码更改为此并查看它是否被调用:

....
IEnumerable<LOL> filter = list.Where(
    delegate(LOL lol) {
        return lol.yes();
    }
).ToList();

string l = "";

Enumerable.Where is a lazy operator -- until you call something that goes through the IEnumerable returned by where (ie. calling .ToList() on it), your function won't get called.

Try changing your code to this and see if it gets called:

....
IEnumerable<LOL> filter = list.Where(
    delegate(LOL lol) {
        return lol.yes();
    }
).ToList();

string l = "";
浪荡不羁 2024-08-01 03:45:34

你必须具体化清单。 添加 ...

filter.ToList();

在声明后 ...,您将遇到断点。 关于我见过的最好的讨论是 这里。 它对惰性评估的公正性比我能做的要好得多。

You have to materialize the list. Add a ...

filter.ToList();

... after the declaration and you will hit your breakpoint. About the best discussion I've seen on that is here. It does lazy evaluation much better justice than I could do.

卖梦商人 2024-08-01 03:45:34

正如其他人所说,您只是定义了您的标准,但还没有要求执行。 这称为延迟加载(如果我错了,请纠正我)。

在过滤器上运行 foreach 循环看看会发生什么。

As others have said, you have just defined your criteria but have not asked it for execution. This is called lazy loading (guys, correct me if I am wrong).

Run a foreach loop on filter to see what happens.

谈情不如逗狗 2024-08-01 03:45:34

乔纳森是对的。

尝试运行此控制台应用程序并在指示的位置设置断点以清楚地看到它。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<LOL> list = new List<LOL>();
            list.Add(new LOL());
            list.Add(new LOL());

            IEnumerable<LOL> filter = list.Where(
                delegate(LOL lol)
                {
                    return lol.yes();
                }
            );

            // Breakpoint #2 will not have been yet.
            Console.Write("No Breakpoint"); // Breakpoint #1 
            // (Breakpoint #2 will now be hit.)
            Console.Write("Breakpoint! " + filter.Count()); 
        }

        class LOL
        {
            public bool yes()
            {
                bool ret = true; // Breakpoint #2
                return ret;
            }

        }

    }
}

Jonathan is correct.

Try running this console application and set breakpoints where indicated to see it clearly.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<LOL> list = new List<LOL>();
            list.Add(new LOL());
            list.Add(new LOL());

            IEnumerable<LOL> filter = list.Where(
                delegate(LOL lol)
                {
                    return lol.yes();
                }
            );

            // Breakpoint #2 will not have been yet.
            Console.Write("No Breakpoint"); // Breakpoint #1 
            // (Breakpoint #2 will now be hit.)
            Console.Write("Breakpoint! " + filter.Count()); 
        }

        class LOL
        {
            public bool yes()
            {
                bool ret = true; // Breakpoint #2
                return ret;
            }

        }

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文