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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:53:36+00:00 2026-05-28T15:53:36+00:00

I want to implement a dictionary that creates its own elements on the fly

  • 0

I want to implement a dictionary that creates its own elements on the fly only when they are accessed (not in advance). To do that I would like to use a getter method, but I simply don’t find any information how to declare a getter in the context of dictionary elements.

I do understand how to add a getter to the whole dictionary (which must return a dictionary when called), but what I want to do is implement a getter that is called when a single element in the dictionary is accessed so I can create that element on the fly. That getter must receive the key that is used for the request as a parameter and it must return the corresponding value.

I do not find any syntax for that task in the docs.

  • 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-28T15:53:37+00:00Added an answer on May 28, 2026 at 3:53 pm

    You just need to reimplement the indexer on Dictionary<,>

        public class MyDictionary<TKey, TValue> : Dictionary<TKey, TValue>
    {
        public new TValue this[TKey key]
        {
            get
            {
                TValue value;
                if (!TryGetValue(key, out value))
                {
                    value = Activator.CreateInstance<TValue>();
                    Add(key, value);
                }
                return value;
            }
            set { base[key] = value; }
        } 
    }
    

    If you need a more sophisticated value instantiation, you can use an activator function

     public class MyDictionary<TKey, TValue> : Dictionary<TKey, TValue>
    {
        readonly Func<TKey, TValue> _activator;
    
        public MyDictionary(Func<TKey, TValue> activator)
        {
            _activator = activator;
        }
    
        public new TValue this[TKey key]
        {
            get
            {
                TValue value;
                if (!TryGetValue(key, out value))
                {
                    value = _activator(key);
                    Add(key, value);
                }
                return value;
            }
            set { base[key] = value; }
        } 
    }
    

    Usage:

    static void Main(string[] args)
    {
        var dict = new MyDictionary<int, string>(x => string.Format("Automatically created Value for key {0}", x));
        dict[1] = "Value for key 1";
        for (int i = 0; i < 3; i++)
        {
            Console.WriteLine(dict[i]);
        }
        Console.Read();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Microsoft has announce that WindowsLiveID become a OpenID provider . I want implement it
I want to create a read-only keyed collection that implements IDictionary<'K, 'V> and IEnumerable<'V>.
I have a dictionary that I read from a plist. I want to create
I want to implement a HashTable (or mabybe a HashSet or Dictionary ) which
I wanted to bind a dictionary to a datagridview. Unfortunately Dictionary does not implement
I want implement in my software solution an VBA editor but in c# 3.0.
given this class definition: public class Frame { IFrameStream CapturedFrom; } I want implement
I want to implement search functionality for a website (assume it is similar to
I want to implement an automatic update system for a windows application. Right now
I want to implement a paperless filing system and was looking to use WIA

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.