Search code examples
c#visual-studio-debuggingfirst-class-functions

Breaking in functions passed to LINQ-functions


I have a function that is passed to Select. But when I put a breakpoint in said function the program does not break.

Example:

public static int PlusTwo(int x)
{
  return x + 2;
}

public static void Main(string[] args)
{
  var foo = new[] { 2, 3, 5, 7 };
  var bar = foo.Select(PlusTwo);
}

Solution

  • That's because of the lazy evaluation. If you try doing a ToList(), the function would evaluate and the break-point would be hit - i.e. try doing:

    var bar = foo.Select(PlusTwo).ToList();