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 261275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:25:06+00:00 2026-05-11T22:25:06+00:00

I have: IDictionary<string, IDictionary<string, IList<long>>> OldDic1; (just for illustration purposes, it is instantiated and

  • 0

I have:

IDictionary<string, IDictionary<string, IList<long>>> OldDic1; 

(just for illustration purposes, it is instantiated and has values – somewhere else)

Why can I do this: ?

Dictionary<string, IDictionary<string, IList<long>>> dic1 = 
  OldDic1 as Dictionary<string, IDictionary<string, IList<long>>>;

Basically dic1 after executing this line has all the values from OldDic1; works.

However when I do this:

Dictionary<string, Dictionary<string, List<long>>> dic1 = 
  OldDic1 as Dictionary<string, Dictionary<string, List<long>>>;

I get null, it is the same as casting except it doesn’t crash and instead it returns null. So the question is why I can’t cast it from the interfaces to types? is there solution, other then changing how it is stored in the first place?

  • 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-11T22:25:06+00:00Added an answer on May 11, 2026 at 10:25 pm

    You can only re-cast the outermost interface/class name, not the generic parameters. The reason your second cast doesn’t work is the same reason you can’t cast from one array type to another even if you can cast the “contained” objects. Here’s why:

    List<object> objects;
    List<string> strings;
    
    objects = strings as List<object>;
    // Uh oh, that's not a string!
    objects.Add(42); 
    
    objects = new List<object> { "The", "answer", "is", 42 };
    // Uh oh, now strings contains an integer?
    strings = objects as List<string>;
    

    The same thing would happen in your second case. You could add some kind of IDictionary to OldDic1 which is not actually a Dictionary, and then dic1 would blow up. It would have a non-Dictionary value. Ruh roh!

    So, when you have containers you can change from IList<X> to List<X> and back as long as X is identical for each.

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

Sidebar

Related Questions

I have a dictionary public static IDictionary<string, IList<string>> checksCollection = new Dictionary<string, IList<string>>(); I
I have a dictionary as follows: IDictionary<string, string> dict; How to create an enumerator
I have the following code set up in my Startup IDictionary<string, string> properties =
Actually i have a little brain bender and and just can't find the right
I have the following code IDictionary<string, IEnumerable<Control>> I need to convert it to IDictionary<string,
i have these 2 classes: public class Category { IDictionary<string, CategoryResorce> _resources; } public
I have a library which returns a hierarchical list composed of IDictionary, IList and
I have the following code string idName = builder.IdentifierName; Disjunction disjunction = Restrictions.Disjunction(); IList
I have multiple lists of strings, IList<string> , that I want to consolidate in
In my asmx file, I have [WebMethod] [ScriptMethod] public void Method(IDictionary<string, CustomClass> objectOfCustomClass) {

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.