开发者

Unable to step into a predicate in VS2008

开发者 https://www.devze.com 2023-04-10 20:53 出处:网络
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};
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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消