无法进入 VS2008 中的谓词

发布于 2024-12-08 07:53:55 字数 1209 浏览 1 评论 0原文

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 技术交流群。

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

发布评论

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

评论(2

你爱我像她 2024-12-15 07:53:55

您需要在谓词中放置一个断点来调试它。
您无法直接“步入”(在我的机器上为 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 the String.Join call.
Long story short: It is .NET framework code that executes these predicates.

流年里的时光 2024-12-15 07:53:55

您预计什么时候进入谓词?不要忘记,当您调用 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 the Where 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.

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