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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:57:06+00:00 2026-05-10T20:57:06+00:00

This is more of an academic inquiry than a practical question. Are there any

  • 0

This is more of an academic inquiry than a practical question. Are there any language or framework features that can, or will in future, allow a heterogeneous typed dcitionary, e.g.

myDict.Add('Name', 'Bill');  myDict.Add('Height', 1.2);  

where myDict now contains not two object types as values, but one string and one double? I could then retrieve my double with

double dbl = myDict['Height']; 

and expect a double or an exception to be thrown?

Please note: The Name and Height values are not necessarily of the same object.

  • 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. 2026-05-10T20:57:06+00:00Added an answer on May 10, 2026 at 8:57 pm

    The only way you’ll be able to do this if you have a custom collection with generic overloads for Add and Get methods. But that would mean you can ask for the wrong type when reading the key out, so it doesn’t gain you much (if anything) over doing the cast yourself when you call your Get method.

    However, if you can push the generic type into the key then that could work. Something like (untested code here)

    sealed class MyDictionaryKey<T> { }  class MyDictionary {     private Dictionary<object, object> dictionary = new Dictionary<object, object>();      public void Add<T>(MyDictionaryKey<T> key, T value)     {         dictionary.Add(key, value);     }      public bool TryGetValue<T>(MyDictionaryKey<T> key, out T value)     {       object objValue;       if (dictionary.TryGetValue(key, out objValue))       {         value = (T)objValue;         return true;       }       value = default(T);       return false;     }      public T Get<T>(MyDictionaryKey<T> key)     {       T value;       if (!TryGetValue(key, out value))          throw new KeyNotFoundException();       return value;     } } 

    Then you can define your keys like:

    static readonly MyDictionaryKey<string> NameKey = new MyDictionaryKey<string>(); static readonly MyDictionaryKey<double> HeightKey = new MyDictionaryKey<double>(); 

    and use it like

    var myDict = new MyDictionary(); myDict.Add(NameKey, 'Bill'); // this will take a string myDict.Add(HeightKey , 1.2); // this will take a double  string name = myDict.Get(NameKey); // will return a string double height = myDict.Get(HeightKey); // will return a double 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is an academic question, more than a practical one. I'm trying to delve
This is more of an academic question about performance than a realistic 'what should
I'm interested in this from more than an academic standpoint rather than a practical
My knowledge of C++ at this point is more academic than anything else. In
This is a bit more of an academic question. Indeed I am preparing for
This is really more of an academic question, but where is this function defined?
I am wondering if there is a way to handle this more elegantly. After
Ok, I'm going to try to make this more clear because my last question
This is more of a design question. I have a template class, and I
This is more of a hypothetical question. I am wondering if anyone knows if/how

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.