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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:38:57+00:00 2026-06-13T01:38:57+00:00

How do I implement AddorUpdate in ConcurrentDictionary so that I can correctly update the

  • 0

How do I implement AddorUpdate in ConcurrentDictionary so that I can correctly update the value, if that value is a collection?

My concern is that since TValue is a reference type I might run into the situation where I call TValue multiple times in a race condition. I would test this myself, but my syntax is wrong so I can’t proceed further.

What must I change to make this work?

   public class TrustList :  ConcurrentDictionary<int, List<TrustRelationshipDetail>>
    {
        public void AddOrUpdateTrustDetail(TrustRelationshipDetail detail)
        {
            List<TrustRelationshipDetail> detailList = new List<TrustRelationshipDetail>();
            detailList.Add(detail);

            this.AddOrUpdate(detail.HierarchyDepth, detailList, (key, oldValue) =>   
            oldValue.Add(detail)  // <--- Compiler doesn't like this, and I think this may cause duplicates if this were to be called...
           );
        }
    }
  • 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-13T01:38:59+00:00Added an answer on June 13, 2026 at 1:38 am

    The purpose of AddOrUpdate() is to replace any existing value with a new value.

    Since you only need to get the existing value (in order to then modify it), you want GetOrAdd():

    this.GetOrAdd(detail.HierarchyDepth, new ConcurrentBag<TrustRelationshipDetail>())
            .Add(detail);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I implement IXmlSerializable for the type below which encodes a RGB color value as
To implement a timer, I'm currently forking a process that uses SIGALRM to periodically
When implement IEditableObject for an entity in wcf ria service, how can I know
I implement search engine with solr that import minimal 2 million doc per day.
Implement multi-level hierarchies so that super user have also all rights that sub user
I implement a reference counting schema and for this i need interlock facilities but
Implement a function punctuation() that takes no parameters, inputs a string from the user,
I implement a function that acts like getline( .. ). So my initial approach
I implement a software application that uses a SQL server database. I'm running a
I implement and support a software applicatoin that allows me to create macros within

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.