调试器未命中断点
我发现了一些很奇怪的东西(我认为!)。 如果我尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Enumerable.Where 是一个惰性运算符 - 除非您调用通过 where 返回的 IEnumerable 的内容(即对其调用 .ToList() ),否则您的函数将不会被调用。
尝试将您的代码更改为此并查看它是否被调用:
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:
你必须具体化清单。 添加 ...
在声明后 ...,您将遇到断点。 关于我见过的最好的讨论是 这里。 它对惰性评估的公正性比我能做的要好得多。
You have to materialize the list. Add a ...
... 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.
正如其他人所说,您只是定义了您的标准,但还没有要求执行。 这称为延迟加载(如果我错了,请纠正我)。
在过滤器上运行 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.
乔纳森是对的。
尝试运行此控制台应用程序并在指示的位置设置断点以清楚地看到它。
Jonathan is correct.
Try running this console application and set breakpoints where indicated to see it clearly.