Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <p>However, if you can push the generic type into the key then that could work. Something like (untested code here)</p> <pre><code>sealed class MyDictionaryKey&lt;T&gt; { } class MyDictionary { private Dictionary&lt;object, object&gt; dictionary = new Dictionary&lt;object, object&gt;(); public void Add&lt;T&gt;(MyDictionaryKey&lt;T&gt; key, T value) { dictionary.Add(key, value); } public bool TryGetValue&lt;T&gt;(MyDictionaryKey&lt;T&gt; 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&lt;T&gt;(MyDictionaryKey&lt;T&gt; key) { T value; if (!TryGetValue(key, out value)) throw new KeyNotFoundException(); return value; } } </code></pre> <p>Then you can define your keys like:</p> <pre><code>static readonly MyDictionaryKey&lt;string&gt; NameKey = new MyDictionaryKey&lt;string&gt;(); static readonly MyDictionaryKey&lt;double&gt; HeightKey = new MyDictionaryKey&lt;double&gt;(); </code></pre> <p>and use it like</p> <pre><code>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 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload