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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T10:56:57+00:00 2026-06-16T10:56:57+00:00

There is a very easy trick which creates a dictionary-like structure where keys are

  • 0

There is a very easy trick which creates a dictionary-like structure where keys are types.
The structure acts like a Dictionary<Type, T?> where keys are Type objects and values are instances of the corresponding types.

This wonderful structure is as fast as just a variable or array since the “lookup” is only done once by the compiler/JITter and the proper value reference is compiled into your program.

    public static class MyDict<T> {
        public static T Value { get; set; }
    }

You can work with that structure like this:

MyDict<string>.Value = MyDict<int>.Value.ToString();

The problem is that this “dictionary” is global. The only way to create different dictionaries is to create different classes.

How can create a similar (fastest “lookup”, no boxing) non-static structure? (Without code generation.)

Simply said: I want to have multiple Dictionary<Type, object>-like objects without lookup costs, casting and boxing.

  • 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-16T10:56:58+00:00Added an answer on June 16, 2026 at 10:56 am

    Ark-kun is using generics to essentially generate unique types at compile time. With a generic type, any static members are unique to that specific closed generic type. This way it’s processed as fast as a standard static member lookup.

    The above usage is equivalent to something like this:

    public static class MyDict_String 
    {
        public static string Value { get; set; }
    }
    
    public static class MyDict_Int32
    {
        public static int Value { get; set; }
    }
    
    MyDict_String.Value = MyDict_Int32.Value.ToString();
    

    AFAIK, types are “static” (in that you can’t define more than one that way) so I don’t know of a way to cheat around this and maintain the same performance of a statically compiled member lookup.

    Your best bet otherwise (I think) is to create a generic instance type that wraps its own dictionary that uses System.Type for its keys and System.Object for its values to which you have to perform boxing/casting when inserting/retrieving values.

    EDIT: Here’s a simple implementation wrapping a dictionary:

    public class MyTypedDict
    {
        private Dictionary<Type, object> Values = new Dictionary<Type, object>();
    
        public T Get<T>()
        {
            object untypedValue;
            if (Values.TryGetValue(typeof(T), out untypedValue))
                return (T)untypedValue;
            return default(T);
        }
    
        public void Set<T>(T value)
        {
            Values[typeof(T)] = value;
        }
    }
    

    Thinking about it more, it might be possible to achieve a more property-like syntax using an ExpandoObject (http://msdn.microsoft.com/en-us/library/system.dynamic.expandoobject.aspx) through some tomfoolery, but I feel like this would be pretty abusive and I can only assume terribly prone to runtime errors. (plus it would afford you nothing at compile time)

    EDITx2: If you really want to have different sets of values, you could nest it within another generic type:

    public static class ValueSets<T>
    {
        public static class MyDict<U>
        {
            public static U Value { get; set; }
        }
    }
    

    With usage like:

    ValueSets<int>.MyDict<string>.Value = "Hello ";
    ValueSets<bool>.MyDict<string>.Value = "World!";
    
    string helloworld = ValueSets<int>.MyDict<string>.Value + ValueSets<bool>.MyDict<string>.Value;
    Console.WriteLine(helloworld);//Hello World!
    

    But then the initial type int and bool in this case become “magical” and without meaning, plus you would need to provide a unique type per distinct set of values you’d like to use. Plus you could not pass it around and modify as an instance variable, rather it’d be statically accessible (so long as you have access to use the type T). So perhaps you could declare minimally visible types that are named with meaning and use those:

    internal class MyFirstWords {}
    internal class MySecondWords {}
    
    ValueSets<MyFirstWords>.MyDict<string>.Value = "Hello ";
    ValueSets<MySecondWords>.MyDict<string>.Value = "World!";
    
    string helloworld = ValueSets<MyFirstWords>.MyDict<string>.Value + ValueSets<MySecondWords>.MyDict<string>.Value;
    Console.WriteLine(helloworld);//Hello World!
    

    Regardless, I think this is quite wacky and I wouldn’t recommend it.

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

Sidebar

Related Questions

There is very important shortcut for "Basic Code Completion" in IntelliJ IDE which assumed
I'm sure there's a very easy explanation for this. What is the difference between
In vim it's very easy to find a file without knowing which directory the
In iOS, there is a very easy and powerful facility to animate the addition
I am sure there is a very easy answer to this but right now
There is probably a very easy solution to my problem but I couldn't find
There is very poor documentation on Extendscript for InDesign online, and I otherwise cannot
There is very little code out there that is in VB, and i'm getting
I was downloading an Pizza pizza application for IPhone. There is very cool animation
gcc 4.4.3 c89 I am just getting started with log4c. However, there is very

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.