One key for multiple dictionary values ​​in C #?

I often need one key for multiple vaules dictionaries, but in C # most of them have two dimensions, such as Dictionary and Hashtable.

I need something like this:

var d = new Dictionary<key-dt,value1-dt,value2-dt,value3-dt,value4-dt>();

dt inside <> means data type. Anyone have any ideas on this?

+5
source share
4 answers

A dictionary is a key-value pair, where the value is selected depending on the key. All keys are unique.

Now, if you want Dictionarywith 1 key and several types of values, you have several options:

should be used first Tuple

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

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

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

System.Dynamic.ExpandoObject may be 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);
   }
}
+13
source

, . .

var dictionary = new Dictionary<TheKeyType, TheValuesType>();

. , , GetHashCode Equals, .

, , , .

var dictionary = new Dictionary<Tuple<Key1Type, Key2Type, Etc>, Tuple<Value1Type, Value2Type, Etc>>();
+4

tuple .

var d = new Dictionary<Tuple<string,string,bool,int>,any-data-typs>();
+1

, Tuple, , / .

, .

+1

All Articles