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

  • SEARCH
  • Home
  • 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 7891431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:37:02+00:00 2026-06-03T06:37:02+00:00

I would think it’s fairly straightforward to cast an IDictionary<TKey, IList<TValue>> object to an

  • 0

I would think it’s fairly straightforward to cast an IDictionary<TKey, IList<TValue>> object to an IDictionary<TKey, IEnumerable<TValue>>, but

var val = (IDictionary<TKey, IEnumerable<TValue>>)Value;

throws a System.InvalidCastException, and

var val = Value as IDictionary<TKey, IEnumerable<TValue>>;

makes val null. What is the proper way to cast this?

  • 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-06-03T06:37:03+00:00Added an answer on June 3, 2026 at 6:37 am

    I would think it’s fairly straightforward to cast an IDictionary<TKey, IList<TValue>> object to an IDictionary<TKey, IEnumerable<TValue>>

    Absolutely not. It wouldn’t be type-safe. Here’s an example of why not:

    // This is fine...
    IDictionary<string, IList<int>> dictionary = new Dictionary<string, IList<int>>();
    
    // Suppose this were valid...
    IDictionary<string, IEnumerable<int>> badDictionary = dictionary;
    
    // LinkedList<T> doesn't implement IList<T>
    badDictionary["foo"] = new LinkedList<int>();
    
    // What should happen now?
    IList<int> bang = dictionary["foo"];
    

    As you can see, that’s going to cause problems – we’d be trying to get a LinkedList<int> out when we expect all the values to implement IList<int>. The point of generics is to be type-safe – so which line would you expect to fail? The first, third and fourth lines look pretty clearly valid to me – so the second one is the only one which can fail to compile, and it does…

    Now in some cases, it can be done safely. For example, you can convert (in C# 4) from IEnumerable<string> to IEnumerable<object> because IEnumerable<T> only uses T in “output” positions.

    See MSDN for more details.

    EDIT: Just to clarify – it’s easy to create a new dictionary with a copy of the existing key/value pairs, e.g. using link:

    var copy = original.ToDictionary<TKey, IEnumerable<TValue>>(pair => pair.Key,
                                                                pair => pair.Value);
    

    You just need to be aware that you now have two separate dictionaries.

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

Sidebar

Related Questions

My problem is one that you would think is quite common, but I haven't
This should be pretty straightforward I would think. I have this string: [quote=Joe Johnson|1]Hi![/quote]
Sorry if this is duplicate,I would think it would be but couldn't find anything.
I would think that this should work...but it doesn't. I get a MissingMemberException. class
You would think there would be a way using DirectCast, TryCast, CType etc but
You would think I would get this done in 10 seconds, but I've spent
You would think this one has been asked before but I cant find it.
Quite a simple one i would think, but i need to be able to
I'm having an issue mapping what I would think is a fairly simple association.
I would think the following piece of code should work, but it doesn't (Edited:

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.