Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6956445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:52:14+00:00 2026-05-27T14:52:14+00:00

Possible Duplicate: In C#, why can't a List<string> object be stored in a List<object>

  • 0

Possible Duplicate:
In C#, why can't a List<string> object be stored in a List<object> variable

Why doesn’t the below work?

List<string> castMe = new List<string>();
IEnumerable<string> getFromCast  = (IEnumerable<string>)castMe; // allowed.

Dictionary<int, List<string>> castMeDict = new Dictionary<int, List<string>>();
Dictionary<int, IEnumerable<string>> getFromDict = (Dictionary<int, IEnumerable<string>>)castMeDict;  // Not allowed

Is this a flaw in the Dictionary casting mechanism, or in my thinking that this should be allowed?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:52:15+00:00Added an answer on May 27, 2026 at 2:52 pm

    Is this a flaw in the Dictionary casting mechanism, or in my thinking that this should be allowed?

    In your thinking. You are expecting that dictionaries should be covariant in their conversions. They are not, for the following reason. Suppose they were, and deduce what could go wrong:

    Dictionary<int, List<string>> castMeDict = 
        new Dictionary<int, List<string>>();
    
    Dictionary<int, IEnumerable<string>> getFromDict = 
        (Dictionary<int, IEnumerable<string>>)castMeDict;
    
    castMeDict[123] = new List<string>();
    IEnumerable<string> strings = getFromDict[123]; // No problem!
    getFromDict[123] = new string[] { "hello" }; // Big problem!
    

    An array of string is convertible to IEnumerable<string> but not to List<string>. You just put something that is not a list of string into a dictionary that can only take list of string.

    In C# generic types may be covariant or contravariant if all the following conditions are met:

    • You’re using C# 4 or better.
    • The varying generic type is an interface or delegate.
    • 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 [here]. See section 23.5.)
    • The type arguments that vary are all reference types.
    • The type has been specifically marked as safe for variance.

    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.

    IEnumerable<T> by contrast does meet all those conditions. You can convert IEnumerable<string> to IEnumerable<object> in C# 4.

    If the subject of variance interests you, consider reading my two dozen articles on the subject:

    http://blogs.msdn.com/b/ericlippert/archive/tags/covariance+and+contravariance/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: In C#, why can't a List<string> object be stored in a List<object>
Possible Duplicate: How can I split an IEnumerable<String> into groups of IEnumerable<string> I have
Possible Duplicate: Converting a string that represents a list, into an actual list object.
Possible Duplicate: How can I get a list of all the defined variables in
Possible Duplicate: Can Read-Only Properties be Implemented in Pure JavaScript? I have an Object
Possible Duplicate: most elegant way to return a string from List<int> I'm not sure
Possible Duplicate: Where can I get a list of all countries/cities to populate a
Possible Duplicate: How can I turn a list into an array in python? How
Possible Duplicate: Python “extend” for a dictionary I know that Python list can be
Possible Duplicate: Modifying C string constants? can you explain why i get access violation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.