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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:18:36+00:00 2026-06-05T10:18:36+00:00

Assume that I have the following object public class MyClass { public ReadOnlyDictionary<T, V>

  • 0

Assume that I have the following object

public class MyClass
{
    public ReadOnlyDictionary<T, V> Dict
    {
        get
        {
            return createDictionary();
        }
    }
}

Assume that ReadOnlyDictionary is a read-only wrapper around Dictionary<T, V>.

The createDictionary method takes significant time to complete and returned dictionary is relatively large.

Obviously, I want to implement some sort of caching so I could reuse result of createDictionary but also I do not want to abuse garbage collector and use to much memory.

I thought of using WeakReference for the dictionary but not sure if this is best approach.

What would you recommend? How to properly handle result of a costly method that might be called multiple times?

UPDATE:

I am interested in an advice for a C# 2.0 library (single DLL, non-visual). The library might be used in a desktop of a web application.

UPDATE 2:

The question is relevant for read-only objects as well. I changed value of the property from Dictionary to ReadOnlyDictionary.

UPDATE 3:

The T is relatively simple type (string, for example). The V is a custom class. You might assume that an instance of V is costly to create. The dictionary might contain from 0 to couple of thousands elements.

The code assumed to be accessed from a single thread or from multiple threads with an external synchronization mechanism.

I am fine if the dictionary is GC-ed when no one uses it. I am trying to find a balance between time (I want to somehow cache the result of createDictionary) and memory expenses (I do not want to keep memory occupied longer than necessary).

  • 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-05T10:18:38+00:00Added an answer on June 5, 2026 at 10:18 am

    There are four major mechanisms available for you (Lazy comes in 4.0, so it is no option)

    1. lazy initialization
    2. virtual proxy
    3. ghost
    4. value holder

    each has it own advantages.

    i suggest a value holder, which populates the dictionary on the first call of the GetValue
    method of the holder. then you can use that value as long as you want to AND it is only
    done once AND it is only done when in need.

    for more information, see martin fowlers page

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

Sidebar

Related Questions

Assume that I have a class that exposes the following event: public event EventHandler
Assume that we have the following code: class Program { static volatile bool flag1;
Assume that I have a particular class of object that defines a class method
Say you have the following object: public class Address { public String Line1 {
Assume that I have a class value object defined in php, where each variable
Assume that I have a .NET Workflow Foundation (WF) SequenceActivity class with the following
Assume the following class: class Person { public string FirstName {get;set;} public string LastName
Assume I have the following class-hierarchy in C++: class AbstractBaseClass { public: // Note:
I have the following code public void MyMethod(object myClass) { if(myClass.GetType().IsSubclassOf(typeof(MyBaseClass))) { // Instantiate
I have to assume that the following method doesn't leak memory: public final void

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.