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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:37:20+00:00 2026-06-01T17:37:20+00:00

I have created dictionary object Dictionary<string, List<string>> dictionary = new Dictionary<string,List<string>>(); I want to

  • 0

I have created dictionary object

Dictionary<string, List<string>> dictionary =
    new Dictionary<string,List<string>>();

I want to add string values to the list of string for a given single key.
If the key doesn’t already exists then I have to add a new key. List<string> is not predefined, I mean I didn’t create any list object and then supplied to dictionary.Add("key",Listname). How to create dynamically this list object in dictionary.Add("key",Listname) and then add strings to this list. If I have to add 100 keys then do I have to create 100 lists before executing dictionary.Add instruction and also do I have to pedefine the contents of this lists ?

Thank you.

  • 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-01T17:37:22+00:00Added an answer on June 1, 2026 at 5:37 pm

    Update: check for existence using TryGetValue to do only one lookup in the case where you have the list:

    List<int> list;
    
    if (!dictionary.TryGetValue("foo", out list))
    {
        list = new List<int>();
        dictionary.Add("foo", list);
    }
    
    list.Add(2);
    

    Original:
    Check for existence and add once, then key into the dictionary to get the list and add to the list as normal:

    var dictionary = new Dictionary<string, List<int>>();
    
    if (!dictionary.ContainsKey("foo"))
        dictionary.Add("foo", new List<int>());
    
    dictionary["foo"].Add(42);
    dictionary["foo"].AddRange(oneHundredInts);
    

    Or List<string> as in your case.

    As an aside, if you know how many items you are going to add to a dynamic collection such as List<T>, favour the constructor that takes the initial list capacity: new List<int>(100);.

    This will grab the memory required to satisfy the specified capacity upfront, instead of grabbing small chunks every time it starts to fill up. You can do the same with dictionaries if you know you have 100 keys.

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

Sidebar

Related Questions

Suppose I have Dictionary like this: Dictionary<string, string> values = new Dictionary<string, string>() {
I have problem with store data in list collection. If I add new data
I have the following classes: public class DictionaryBuilder<T> where T : IDictionary<string, object>, new()
I have a Dictionary of objects I have created in smalltalk, which I am
I have defined a class Listener and created a dictionary of Listener objects. Each
I have created a dictionary in Class A and would like to return the
I am trying to implement vectorization of a text file...I have created a dictionary
I have a dictionary as follows: IDictionary<string, string> dict; How to create an enumerator
The code: [1] private delegate void ThreadStatusCallback(ReceiveMessageAction action, Dictionary<int, List<string>> message); [2] Dictionary<int, List<string>>
I want to deserialize a JSON object (using GSON, because I already use it

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.