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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:02:11+00:00 2026-05-20T05:02:11+00:00

I have a ConcurrentDictionary: private static ConcurrentDictionary<int, string> _cd = new ConcurrentDictionary<int, string>(); To

  • 0

I have a ConcurrentDictionary:

private static ConcurrentDictionary<int, string> _cd = new ConcurrentDictionary<int, string>();

To be conservative, the actual key to be used to retrieve items is an object but I instead simply use its hash code as the key so that a (potentially large) object isn’t the key:

public static string GetTheValue(Foo foo)
{
    int keyCode = foo.GetHashCode(); // GetHashCode is overridden to guarantee uniqueness

    string theValue = _cd.GetOrAdd(keyCode, FooFactory);

    return theValue;
}

However, I need various properties in the Foo object when preparing an object in the Factory:

private static string FooFactory(Foo foo)
{
    string result = null;

    object propA = foo.A;
    object propB = foo.B;

    // ... here be magic to set result

    return result;
}

Since the valueFactory parameter of GetOrAdd() expects Func<int, string>, it appears that I cannot pass my Foo object to it. Is it at all possible to do so?

  • 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-20T05:02:11+00:00Added an answer on May 20, 2026 at 5:02 am

    I think there is a fundamental misunderstanding here:

    To be conservative, the actual key to
    be used to retrieve items is an object
    but I instead simply use its hash code
    as the key so that a (potentially
    large) object isn’t the key
    .

    I’ve bolded the phrases that I think you think are related. They’re really not. If you’re picturing your dictionary as being too large because its keys are these big objects, you’re picturing it wrong. For reference types (any class in C#), the keys will be stored in the dictionary only as references, which are the size of a whopping integer. And if you’re concerned about passing the keys between methods, again: it’s not what you think. Only references will be copied and passed around, not the objects themselves.

    So I agree with SLaks: just use your Foo type (or whatever it’s really called) as the key in the first place. It will make your life a lot simpler.

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

Sidebar

Related Questions

let's say I have the following code: ConcurrentDictionary<long, long> myDict= new ConcurrentDictionary<long, long>(); Normally
I have a ConcurrentDictionary object that I would like to set to a Dictionary
Have you used VS.NET Architect Edition's Application and System diagrams to start designing a
I started to use the new ConcurrentDictionary from .Net4 to implement a simple caching
Have I correctly added an element to a hashTable? Flows flows = new Flows(sIP,dIP);
I have a new web app that is packaged as a WAR as part
I have found this example on StackOverflow: var people = new List<Person> { new
As MSDN says ConcurrentDictionary<TKey, TValue> Class Represents a thread-safe collection of key-value pairs that
Have just started using Google Chrome , and noticed in parts of our site,
Have you ever seen any of there error messages? -- SQL Server 2000 Could

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.