无法进入 VS2008 中的谓词
class Program
{
static void Main(string[] args)
{
var numbers = new[] {1, -1, -2, 3.5, 1.1, -0.1, 2, 5.7, 8, 9, -10, -2};
Func<double, bool> positiveIntegerSelector = x =>
{
if(x < 0)
return false;
var temp = (int) x;
return temp == x;
};
Func<double, bool> negativeIntegerSelector = x =>
{
if(x >= 0)
return false;
var temp = (int) x;
return temp == x;
};
var positiveIntegers = numbers.Where(positiveIntegerSelector); //unable to step in
var negativeIntegers = numbers.Where(negativeIntegerSelector);
Console.WriteLine(String.Join(",", positiveIntegers.Select(x => x.ToString()).ToArray()));
Console.WriteLine(String.Join("," , negativeIntegers.Select(x => x.ToString()).ToArray()));
}
}
我在这里缺少什么? (除了断点)
请注意,它调试得很好,否则除了我无法介入的谓词之外。
编辑
为自己是一个绝对的混蛋而道歉——> stepout shift + F11(当谓词被指定为方法组/委托时)
当谓词被指定为 lambda 表达式时,step in 照常工作 x => PositiveIntegerSelector (x) as one 可以在 lambda 表达式中的谓词上指定一个中断点
class Program
{
static void Main(string[] args)
{
var numbers = new[] {1, -1, -2, 3.5, 1.1, -0.1, 2, 5.7, 8, 9, -10, -2};
Func<double, bool> positiveIntegerSelector = x =>
{
if(x < 0)
return false;
var temp = (int) x;
return temp == x;
};
Func<double, bool> negativeIntegerSelector = x =>
{
if(x >= 0)
return false;
var temp = (int) x;
return temp == x;
};
var positiveIntegers = numbers.Where(positiveIntegerSelector); //unable to step in
var negativeIntegers = numbers.Where(negativeIntegerSelector);
Console.WriteLine(String.Join(",", positiveIntegers.Select(x => x.ToString()).ToArray()));
Console.WriteLine(String.Join("," , negativeIntegers.Select(x => x.ToString()).ToArray()));
}
}
What am'i missing here ? (other than breakpoints)
pls note it debugs fine just fine otherwise except predicate in which i'm unable to step in.
edit
apologies for being an absolute jackass --> stepout shift + F11 (when predicate is specified as a method group / delegate)
step in works as usual when predicate is specified as a lambda expression
x => positiveIntegerSelector (x) as one can then specify a break pt on the predicate in the lambda expression
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在谓词中放置一个断点来调试它。
您无法直接“步入”(在我的机器上为 F11)谓词,因为调用谓词的代码不是您的代码,而是在
Enumerable.Where< 中创建的代码/code>,一旦迭代结果就会执行。在您的情况下,这些是带有
String.Join
调用的行。长话短说:执行这些谓词的是 .NET 框架代码。
You need to put a breakpoint into the predicate to debug it.
You can't "step into" (F11 on my machine) the predicate directly, because the code that invokes the predicate is not your code but the code that gets created inside
Enumerable.Where
, which will be executed as soon as you iterate the result. In your case, these are the lines with theString.Join
call.Long story short: It is .NET framework code that executes these predicates.
您预计什么时候进入谓词?不要忘记,当您调用
Where
时,当时不会执行任何过滤 - 它只是创建一个代表过滤后的对象顺序。如果您期望能够在Where
调用上按 F11,那么您肯定无法到达您的谓词。我刚刚在 Visual C# Express 2010 中尝试过此操作,在每个 lambda 表达式的第一个语句上放置一个断点,并且在程序的最后两行期间运行谓词时,它毫无问题地命中断点。我希望 VS2008 也是如此。
When were you expecting to step into the predicate? Don't forget that when you call
Where
, that doesn't perform any filtering at the time - it just creates an object representing the filtered sequence. If you were expecting to be able to hit F11 on theWhere
call, you certainly wouldn't get to your predicate.I've just tried this in Visual C# Express 2010, putting a break point on the first statement of each lambda expression, and it hit the breakpoints with no problem as it ran the predicates during the last two lines of the program. I would expect the same to be true for VS2008.