Mike-Ward.Net

Improving your code with LINQ and Lambdas

I’m surprised at how much code I review that still does not take advantage of the newer constructs of C#. Here’s an (admittedly contrived) example.

static void Main()
{
    var names = new[] { "name1", "name2", "name3", "name4", "name5", "name6", "name7" };
    var count = 0;
    foreach (var name in names)
    {
        if (name.Contains("name"))
            count += 1;
    }
    Console.WriteLine(count);
}

Now compare it to this.

static void Main()
{
    var names = new[] { "name1", "name2", "name3", "name4", "name5", "name6", "name7" };
    Console.WriteLine(names.Count(name => name.Contains("name")));
}

Which would you prefer to maintain? Which one more clearly communicates the intent of the program?

If that’s not enough to convince you, consider that extension methods like Count() are static, which allows for further optimization and parallel execution opportunities.

If you consider yourself a pro, you owe it to yourself (and your fellow developers) to keep on top of these things. Read books, pair program, embrace the changes and learn from others. People smarter than you and I have spent countless hours thinking about these things and improving them. Shouldn’t you take advantage of those efforts?

← newer older →
.Net, Technology, Life, Whatever

Recent Posts

Quick Tip - Clear ReSharper's Cache to Fix False Errors
Quick Tip - Refined GitHub
Vertical Layout for HTML Tables
Visual Studio 2017 Installer Quick Tip
VSColorOutput for VS 2017 Released
Quick ReSharper tip for inline templates
NoSQL vs. NewSQL: What's the Difference?
More... (1077)

Donate with PayPal