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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:48:31+00:00 2026-06-14T02:48:31+00:00

I want to create a dictionary of the following type, var data = new

  • 0

I want to create a dictionary of the following type,

var data = new Dictionary<string, Dictionary<string, Dictionary<string, int>>>();

when I try to add value to the dictionary in the following way, I get a KeyNotFound Exception.

data[key1][key2][key3]= 3;

What am I doing wrong here? I assume if a key is not found in a dictionary, it is automatically added to it.

otherwise Is there any way to add keys at runtime?

I expect output of following type:

 [male,[animal,[legs,4]

               [eyes,2]]
       [human,[hands,2]
               [ears,2]]

[female,[animal,[nose,1]

                 [eyes,2]]
         [bird,[wings,2]
                 [legs,2]]
  • 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-14T02:48:32+00:00Added an answer on June 14, 2026 at 2:48 am

    The problem here is that you are only trying to assign to the innermost dictionary. But the two outer levels does not exist.

    You need to be explicit about each level:

    if (!data.ContainsKey(key1))
        data[key1]  = new Dictionary<string, Dictionary<string, int>();
    

    … and so on. It can be a bit cumbersome to write, so if you need it a lot, I suggest you create an extension method on dictionary that lets you do this easily.

    If you assign to a key in a dictionary, that entry is created. But if you read from a key in a dictionary, and that key does not exist, you get an exception.

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

Sidebar

Related Questions

I want to create the following data-structure with js: folders dictionary: folder-id(guid) : ==>
I want to create the dictionary of all the ViewModels. public static Dictionary<string, WeakReference>
Say I have a javascript dictionary variable with the following key value pairs: var
In my class, I want to use a dictionary with the following declaration: Dictionary<string,
I want to create a Python dictionary which holds values in a multidimensional array
i want create a custom json data from the mssql 2008 results so that
i want create Dynamic Slideshow in Jquery i'm Write this code var ctx =
I'm looking for a way to create a dictionary without writing the key explicitly.
I have a dictionary that I read from a plist. I want to create
I'm following these instructions to create a custom dictionary. Its content looks like 2.0

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.