One Key to multiple values dictionary in C#?

c#

Solution

A dictionary is a key-value pair, where the value is fetched depending on the key. The keys are all unique.

Now if you want a `Dictionary` with 1 keytype and multiple value types, you have a few options:

first is to use a `Tuple`

`var dict = new Dictionary<KeyType, Tuple<string, string, bool, int>>()`

The other is to use (with C# 4.0 and above):

`var dict = new Dictionary<KeyType, dynamic>()`

the `System.Dynamic.ExpandoObject` can have value of any type.

using System;
using System.Linq;
using System.Collections.Generic;

public class Test {
   public static void Main(string[] args) {
        dynamic d1 = new System.Dynamic.ExpandoObject();
    var dict = new Dictionary<int, dynamic>();
        dict[1] = d1;
        dict[1].FooBar = "Aniket";
        Console.WriteLine(dict[1].FooBar);
        dict[1].FooBar = new {s1="Hello", s2="World", s3=10};
        Console.WriteLine(dict[1].FooBar.s1);
        Console.WriteLine(dict[1].FooBar.s3);
   }
}

Problem

I often need one key to multiple vaules dictionary, but in C# most of them are two dimensions like Dictionary and Hashtable. I want something like this: ``` var d = new Dictionary<key-dt,value1-dt,value2-dt,value3-dt,value4-dt>(); ``` dt inside<> means data type. Anybody has ideas about this?

Original source

Related problems