Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to skip the function with lambda code inside?

Consider the fragment below:

    [DebuggerStepThrough]
    private A GetA(string b)
    {
        return this.aCollection.FirstOrDefault(a => a.b == b);
    }

If I use F11 debugger doesn't skip the function instead it stops at a.b == b.

Is there any way to jump over this function rather than using F10?

like image 214
drumsta Avatar asked Dec 14 '10 23:12

drumsta


2 Answers

IMO this is a bug in the C# compiler. The compiler should also place those attributes on the anonymous methods. The workaround is to fallback to doing the work manually that the C# compiler does for you:

[DebuggerStepThrough]
private A GetA(string b)
{
    var helper = new Helper { B = b };

    return this.aCollection.FirstOrDefault(helper.AreBsEqual);
}

private class Helper
{
    public string B;

    [DebuggerStepThrough]
    public bool AreBsEqual(A a) { return a.b == this.B; }
}

But of course this is nasty and quite unreadable. That's why the C# compiler should have done this. But the difficult question for the C# team is of course: which of the attributes that you place on a method must be copied to internal anonymous methods and which should not?

like image 71
Steven Avatar answered Nov 02 '22 14:11

Steven


I can see why it happens but don't have a way to get around it. Perhaps someone can build on this. The lambda expression gets compiled into an Anonymous Method.

I see: Program.GetA.AnonymousMethod__0(Test a)

Just like if you called another method in the method you've shown, pressing F11 would go into that method. eg/

[DebuggerStepThrough]
static A GetA<A>(IList<A> aCollection, string b) where A : Test
{
    DoNoOp();
    return aCollection.FirstOrDefault(a => a.b == b);
}

static void DoNoOp()
{
    // noop
    Console.WriteLine("got here");
}
like image 45
Tim Carter Avatar answered Nov 02 '22 14:11

Tim Carter