Search code examples
c#linq

Using Linq to objects, how to create an empty dictionary of <string, string> easily?


To create an empty sequence one uses the following

var empty = Enumerable.Empty<string> ();

Is there an equivalent for creating an empty dictionary as easily as this?


Solution

  • No there is no equivalent...

    The purpose of Enumerable.Empty<T>() is to return a "cached" instance of an empty array. So you can avoid the overhead of creating a new array (return new T[0];).

    You cannot translate this to a non-readonly structure like a IDictionary<TKey, TValue> or Dictionary<TKey, TValue> since the returned instance might be modified later and would therefore invalidate the purpose...