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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:38:52+00:00 2026-05-31T22:38:52+00:00

I am using a dictionary in C# to store some key-value pairs and had

  • 0

I am using a dictionary in C# to store some key-value pairs and had a question on the best way to populate the dictionary.

I need to do some other operations in order to find and add my key-value pairs to my dictionary. After those operations I may have found a key-value to add to the dictionary or I could have found nothing.

My question is how I should populate the dictionary?

  • Should I use a function that returns a key-value pair if found and otherwise an empty one contained within a dictionary.Add(function()) call? I don’t want to add an empty key-value pair to the dictionary, so I’m not sure how the return call for that function would work, or

  • Should I pass the dictionary to the function and add to it if needed? Like:

    function(dictionary) 
    {
        if (pair found)
        {
            dictionary.add(pair)
        }
    }
    
  • 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-31T22:38:53+00:00Added an answer on May 31, 2026 at 10:38 pm

    Not sure what you ask here, but here is how I handle dictionary to either add or update a value based on a key:

    string key = "some key here";
    string value = "your value";
    if (myDict.ContainsKey(key))
    {
        myDict[key] = value;
    }
    else
    {
        myDict.Add(key, value);
    }
    

    You can wrap this in a method if you like:

    void AddOrUpdate(Dictionary<string, string> dict, string key, string value)
    {
        if (dict.ContainsKey(key))
        {
            dict[key] = value;
        }
        else
        {
            dict.Add(key, value);
        }
    }
    
    //usage:
    AddOrUpdate(myDict, "some key here", "your value");
    

    You can also use the TryGetValue method but can’t see any obvious advantage in this.

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

Sidebar

Related Questions

I'd like to call a function in python using a dictionary with matching key-value
Currently I am using a Dictionary<int,node> to store around 10,000 nodes. The key is
I'm using a .resx resource file to store some dictionary data, wich i want
I have a Dictionary object that is formed using a double as its key.
I am using ZODB to store some data that exists in memory for the
I have to work on some code that's using generic lists to store a
What is the best way to store an NSTableView 's settings (ideally leveraging its
Need advice: I am implementing ID3 algorithm in Machine Learning. I am using dictionary
I'm using a Dictionary<int, MyType> in a class. That class implements a interface that
Possible Duplicate: Which collection for storing unique strings? I am currently using a Dictionary<string,

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.