18

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?

diwatu
  • 5,641
  • 5
  • 38
  • 61
  • 2
    I'm assuming you want a key value pair with the value being a list? It's valid to do: `var d = new Dictionary>();` or whatever you want to store a list of. – Justin Chmura Feb 20 '13 at 18:23
  • 2
    You'll need to explain what you want a bit better. The current explanation doesn't really say anything. Give an example of how you might use the dictionary you envision. – Pete Feb 20 '13 at 18:23
  • The Dictionary requires 2 Arguments so what is it you are ultimately trying to achieve here..? – MethodMan Feb 20 '13 at 18:23
  • Thanks Justin, you are right.it dosen't show correctly here due to some special charactores in my content. – diwatu Feb 20 '13 at 18:24
  • What is the key of your dictionary? The string? Or everything but the last argument? – Pete Feb 20 '13 at 18:26
  • See this similar question for some ideas http://stackoverflow.com/questions/689940/hashtable-with-multidimensional-key-in-c-sharp – Drahcir Feb 20 '13 at 18:28
  • So many down votes? It's not a bad question, it's just a duplicate. – Gert Arnold Feb 20 '13 at 18:40
  • @GertArnold It is a bad question because it's entirely ambigous as to what he wants. Does he expect a multi-keyed dictionary, a dictionary with one key and multiple values, does he expect multiple keys *and* multiple values, etc. He did not respond to the clarifying questions and instead left the question as is despite lots of confusion. That's a poor quality question. – Servy Feb 20 '13 at 18:45
  • using Tuple as the value part in Dictionary is whay i want. – diwatu May 04 '13 at 06:49

4 Answers4

30

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);
   }
}
Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
  • can you please explain this one `dict[1] = d1; dict[1].FooBar = "Aniket";`? as I understood `dic[1].FooBar = Aniket` is the key for dictionary, but what does `FooBar` in that case mean? – vladimir Oct 12 '17 at 13:48
  • @vladimir the `.FooBar` in this case means it can be anything at all. You can take the same code and replace `.FooBar` with `.vladimir` and it would still work. I have dynamically, at runtime, created a property in `dict[1]` (which points to `d1`) called `FooBar` – Aniket Inge Oct 12 '17 at 16:56
  • can we initialize DICT out of method as global variable e.g ` { "Sweden", currentRate = 9.95 }, { "Sweden", currency = "SEK" } ` – Ravi Parekh May 08 '22 at 20:51
  • 1
    this is a great solution. kinda turns C# into javascript. Thanks – BoundForGlory Jun 11 '22 at 16:41
7

Describe both the appropriate key fields and the appropriate value fields with classes. and use a dictionary of those types.

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

Note: If you have multiple values acting as the key, you would define a class to encapsulate those values and provide proper overrides of GetHashCode and Equals so that the dictionary could recognize their equality.

Short of doing this, you can utilize tuples, but you want to limit this pattern, as tuples are non self-describing.

var dictionary = new Dictionary<Tuple<Key1Type, Key2Type, Etc>, Tuple<Value1Type, Value2Type, Etc>>();
Anthony Pegram
  • 123,721
  • 27
  • 225
  • 246
4

Instead of using a Tuple, which is a perfectly valid solution, i'd advise on creating your own class as the key and/or Value.

You might realize the tuple will become an hard to read code.

Luis Filipe
  • 8,488
  • 7
  • 48
  • 76
1

Use a tuple as a key.

var d = new Dictionary<Tuple<string,string,bool,int>,any-data-typs>();
System Down
  • 6,192
  • 1
  • 30
  • 34
  • 4
    I would suggest rather using one of those types as key, and Tuple<...> as value. – Lukasz M Feb 20 '13 at 18:29
  • Thank you, this is what i want. I will use it like this: var d = new Dictionary>(); – diwatu Feb 20 '13 at 18:29
  • 1
    @LukaszM - the way I understood it was that he wanted a composite key. – System Down Feb 20 '13 at 18:30
  • @Giswin - If what you want is a composite key, then this will work. It could get a bit ugly as tuples are not self-describing though. – System Down Feb 20 '13 at 18:31
  • I see. I suppose he just wanted a collection that associates together more than two values. Anyway, `Tuple` class can be used in both cases :). – Lukasz M Feb 20 '13 at 18:34
  • Yes, in my case because I want a one key to multiple values data set, var d = new Dictionary>(); This is what i want. – diwatu Feb 20 '13 at 20:15
  • Yep, don't use a Tuple for the key, just the value. Because it's what the OP wanted anyway. – John Stock Oct 30 '20 at 03:43