Enumerate Dictionary.Values vs Dictionary itself : <p>I was exploring the sources of ASP.NET core on GitHub to see what kind of tricks the ASP.NET team used to speed up the framework. I saw something that intrigued me. In the source code of the <a href="https://github.com/aspnet/DependencyInjection/blob/dev/src/Microsoft.Extensions.DependencyInjection/ServiceProvider.cs" rel="noreferrer">ServiceProvider</a>, in the Dispose implementation, they enumerate a dictionary, and they put a comment to indicate a performance trick :</p>



<pre><code>private readonly Dictionary&lt;IService, object&gt; _resolvedServices = new Dictionary&lt;IService, object&gt;();

// Code removed for brevity

public void Dispose()    
{        
    // Code removed for brevity

    // PERF: We've enumerating the dictionary so that we don't allocate to enumerate.
    // .Values allocates a KeyCollection on the heap, enumerating the dictionary allocates
    // a struct enumerator
    foreach (var entry in _resolvedServices)
    {
        (entry.Value as IDisposable)?.Dispose();
    }

    _resolvedServices.Clear();        
}
</code></pre>

<p>What is the difference if the dictionary is enumerated like that ?</p>

<p></p>

<pre><code>foreach (var entry in _resolvedServices.Values)
{
    (entry as IDisposable)?.Dispose();
}
</code></pre>

<p>It has a performance impact ? Or it's because allocate a <a href="https://msdn.microsoft.com/en-us/library/x8bctb9c(v=vs.110).aspx" rel="noreferrer">ValueCollection</a> will consume more memory ?</p>
