The Cost of Optimization

Imagine you were to write a query as follows:

var groups = new Dictionary<string, List<Process>>();foreach (var proc in Process.GetProcesses()){    if (proc.ProcessName.StartsWith("d"))    {        List<Process> bucket;        if (groups.TryGetValue(proc.ProcessName, out bucket))            bucket.Add(proc);        else            groups[proc.ProcessName] = new List<Process>() { proc };    }}

It’s most likely not immediately apparent to you this is carrying out a filtering operation as well as a grouping operation, let alone that the runtime would be able to see our intent from the rather complex preceding code. Also because you’ve prescribed the recipe to execute your algorithm very precisely, ...

Get C# 5.0 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.