Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>Is this a flaw in the Dictionary casting mechanism, or in my thinking that this should be allowed?</p> </blockquote> <p>In your thinking. You are expecting that dictionaries should be <em>covariant</em> in their conversions. They are not, for the following reason. Suppose they were, and deduce what could go wrong:</p> <pre><code>Dictionary&lt;int, List&lt;string&gt;&gt; castMeDict = new Dictionary&lt;int, List&lt;string&gt;&gt;(); Dictionary&lt;int, IEnumerable&lt;string&gt;&gt; getFromDict = (Dictionary&lt;int, IEnumerable&lt;string&gt;&gt;)castMeDict; castMeDict[123] = new List&lt;string&gt;(); IEnumerable&lt;string&gt; strings = getFromDict[123]; // No problem! getFromDict[123] = new string[] { "hello" }; // Big problem! </code></pre> <p>An array of string is convertible to <code>IEnumerable&lt;string&gt;</code> but not to <code>List&lt;string&gt;</code>. You just put something that is <em>not</em> a list of string into a dictionary that can only take list of string.</p> <p>In C# generic types may be covariant or contravariant if all the following conditions are met:</p> <ul> <li>You're using C# 4 or better.</li> <li>The varying generic type is an interface or delegate.</li> <li>The variance is provably typesafe. (The C# specification describes the rules we use to determine variance safety. C# 4.0 Version doc file can be downloaded <a href="http://www.google.com/#q=c%23+4+specification+variance+safety">[here]</a>. See section 23.5.)</li> <li>The type arguments that vary are all reference types.</li> <li>The type has been specifically marked as safe for variance.</li> </ul> <p>Most of those conditions are not met for dictionary -- it is not an interface or delegate, it is not provably safe, and the type is not marked as safe for variance. So, no variance for dictionaries.</p> <p><code>IEnumerable&lt;T&gt;</code> by contrast does meet all those conditions. You can convert <code>IEnumerable&lt;string&gt;</code> to <code>IEnumerable&lt;object&gt;</code> in C# 4.</p> <p>If the subject of variance interests you, consider reading my two dozen articles on the subject:</p> <p><a href="http://blogs.msdn.com/b/ericlippert/archive/tags/covariance+and+contravariance/">http://blogs.msdn.com/b/ericlippert/archive/tags/covariance+and+contravariance/</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload