Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a Dictionary that holds different types in C#

I need some sort of way to store key/value pairs where the value can be of different types.

So I like to do:

 int i = 12;  string s = "test";  double x = 24.1;   Storage.Add("age", i);  Storage.Add("name", s);  Storage.Add("bmi", x); 

And later retrieve the values with:

 int a = Storage.Get("age");  string b = Storage.Get("name");  double c = Storage.Get("bmi"); 

How should a Storage like this look like? Thanks, Erik

like image 476
Enrico Avatar asked Nov 05 '10 11:11

Enrico


People also ask

Can a dictionary hold different data types?

One can only put one type of object into a dictionary. If one wants to put a variety of types of data into the same dictionary, e.g. for configuration information or other common data stores, the superclass of all possible held data types must be used to define the dictionary.

Can dictionary have different data types in C#?

Well, you could use Dictionary<string, dynamic> in C# 4 / . NET 4 - but other than that, you can't do it with exactly the code shown because there's no type which is implicitly convertible to int , string and double . (You could write your own one, but you'd have to list each type separately.)

Can a dictionary hold different types Python?

For example, you can use an integer, float, string, or Boolean as a dictionary key. However, neither a list nor another dictionary can serve as a dictionary key, because lists and dictionaries are mutable. Values, on the other hand, can be any type and can be used more than once.

Can a dictionary have duplicate keys C?

In Dictionary, the key cannot be null, but value can be. In Dictionary, key must be unique. Duplicate keys are not allowed if you try to use duplicate key then compiler will throw an exception. In Dictionary, you can only store same types of elements.


2 Answers

Well, you could use Dictionary<string, dynamic> in C# 4 / .NET 4 - but other than that, you can't do it with exactly the code shown because there's no type which is implicitly convertible to int, string and double. (You could write your own one, but you'd have to list each type separately.)

You could use Dictionary<string, object> but then you'd need to cast the results:

int a = (int) Storage.Get("age"); string b = (string) Storage.Get("name"); double c = (double) Storage.Get("bmi"); 

Alternatively, you could make the Get method generic:

int a = Storage.Get<int>("age"); // etc 
like image 188
Jon Skeet Avatar answered Sep 19 '22 15:09

Jon Skeet


You could declare a Dictionary containing just the type object and then cast your results; .e.g.

Dictionary<string, object> storage = new Dictionary<string,object>();  storage.Add("age", 12); storage.Add("name", "test"); storage.Add("bmi", 24.1);  int a = (int)storage["age"]; string b = (string)storage["name"]; double c = (double)storage["bmi"]; 

However, this isn't that elegant. If you know you are always going to be storing age, name, bmi I would create an object to encapsulate those and store that instead. E.g.

public class PersonInfo {     public int Age { get; set; }     public string Name { get; set; }     public double Bmi { get; set; } } 

And then use that insead of the Dictionary... e.g.

PersonInfo person1 = new PersonInfo { Name = "test", Age = 32, Bmi = 25.01 };  int age = person1.Age; 

etc.

like image 45
Steve Avatar answered Sep 22 '22 15:09

Steve