Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# LINQ Debugging through an IEnumerable extension method

Tags:

c#

debugging

linq

I am trying to step through the following IEnumerable extension method that I called DumpIt. The debugger will not step through it. How can I get the debugger to step into this extension method?

My unit test:

[Test]
        public void TestDumpIt()
        {
            var words =
                new string[] {"   KOOKABURRA", "Frogmouth", "kingfisher   ", "loon", "merganser"};

            var result = words
                .DumpIt(d => "original: " + d)
                .Select(word => word.Trim())
                .DumpIt(d => "trimmed: " + d)
                .Select(word => word.ToLower())
                .DumpIt(d => "lower cased: " + d)
                .Where(word => word.StartsWith("k"))
                .DumpIt(d => "starts with k: " + d)
                .OrderBy(word => word)
                .DumpIt(d => "orderd: " + d);
        }

The extension method:

public static IEnumerable<T> DumpIt<T>(this IEnumerable<T> input,
    Func<T, string> toString
    )
{
    foreach (var item in input)
    {
        Output.ActivePane.Activate();
        Output.ActivePane.OutputString(
            ShowWhiteSpace ? '[' + toString(item) + ']' : toString(item));

        yield return item;
    }
}

Thanks for any help!

like image 280
Frekster Avatar asked Dec 27 '22 15:12

Frekster


1 Answers

When using yield, you are using delayed execution. The other LINQ methods you are using use delayed execution as well. The DumpIt method won't actually get called until you start enumerating your result in TestDumpIt. If you want to see the code get called, you need to enumerate the enumerable. For example, you could just add a result.ToList() at the end to force enumeration.

like image 117
Matt Smith Avatar answered Dec 31 '22 14:12

Matt Smith