kalki
kalki

Reputation: 526

Unable to step into a predicate in 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()));
    }
}

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

Upvotes: 0

Views: 681

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1500953

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.

Upvotes: 1

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174329

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.

Upvotes: 1

Related Questions