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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:12:07+00:00 2026-05-14T06:12:07+00:00

I am sick of doing blocks of code like this for various bits of

  • 0

I am sick of doing blocks of code like this for various bits of code I have:

if (dict.ContainsKey[key]) {  
    dict[key] = value;  
}  
else {  
    dict.Add(key,value);  
}

and for lookups (i.e. key -> list of value)

if (lookup.ContainsKey[key]) {  
    lookup[key].Add(value);  
}  
else {  
    lookup.Add(new List<valuetype>);  
    lookup[key].Add(value);  
}  

Is there another collections lib or extension method I should use to do this in one line of code no matter what the key and value types are?

e.g.

dict.AddOrUpdate(key,value)  
lookup.AddOrUpdate(key,value)
  • 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-14T06:12:07+00:00Added an answer on May 14, 2026 at 6:12 am

    As Evgeny says, the indexer will already replace existing values – so if you just want to unconditionally set the value for a given key, you can do

    dictionary[key] = value;
    

    The more interesting case is the “get a value, or insert it if necessary”. It’s easy to do with an extension method:

    public static TValue GetOrCreateValue<TKey, TValue>
        (this IDictionary<TKey, TValue> dictionary,
         TKey key,
         TValue value)
    {
        return dictionary.GetOrCreateValue(key, () => value);
    }
    
    public static TValue GetOrCreateValue<TKey, TValue>
        (this IDictionary<TKey, TValue> dictionary,
         TKey key,
         Func<TValue> valueProvider)
    {
        TValue ret;
        if (!dictionary.TryGetValue(key, out ret))
        {
            ret = valueProvider();
            dictionary[key] = ret;
        }
        return ret;
    }
    

    Note the use of a delegate to create the default value – that facilitates scenarios like the “list as value” one; you don’t want to create the empty list unless you have to:

    dict.GetOrCreateValue(key, () => new List<int>()).Add(item);
    

    Also note how this only performs the lookup once if the key is already present – there’s no need to do a ContainsKey and then look up the value. It still requires two lookups when it’s creating the new value though.

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

Sidebar

Related Questions

I have been sick and tired Googling the solution for doing case-insensitive search on
when i have a link like this which can be longer but i've put
I'm getting sick of boilerplate immutable value object code. Would either PostSharp or T4
Code: @albums = @genres.each_with_index { |item,key| if item.keys.include?('Albums') break end } This should be
I'm getting sick with this problem. I have some tables that have contentes launched
I'm getting sick of various annoyances about Windows 7 like permissions and crap like
Okay. I'm sick of this problem. This has to have an easy fix, I'm
I'm sick of getting This client is too old to work with working copy
Help, Im really sick of using this lots of nbsps in my results page.
Okay, so I am sick of writing this... res = Something.objects.filter(asdf=something) if res: single

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.