Austin Salonen
Austin Salonen

Reputation: 50225

Preventing lambdas from being stepped into when hosting method is DebuggerHidden

private static void Main()
{
    Console.WriteLine(GetRandomInteger());
    Console.ReadLine();
}

[DebuggerHidden]
private static int GetRandomInteger()
{
    Func<int> random = () => 4;
    return GetRandomInteger(random);
}

[DebuggerHidden]
private static int GetRandomInteger(Func<int> random)
{
    return random();
}

Using the code above, is there a way to prevent the Func<int> random = () => 4; line from getting stepped into when debugging?

Upvotes: 5

Views: 349

Answers (2)

Joel Rondeau
Joel Rondeau

Reputation: 7586

[DebuggerHidden] can be used on a property, and this property appears to behave as you would like:

[DebuggerHidden]
private static Func<int> random
{
  get { return () => 4; }
}

It is a workaround, like the other answer; however, it keeps the lambda and may be closer to the original intent.

Upvotes: 2

Christopher Stevenson
Christopher Stevenson

Reputation: 2861

Well you could use a private function with the [DebuggerHidden] attribute instead of a labmda, and set the Func<int> delegate to the private function.

Upvotes: 6

Related Questions