Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi

how can create my own dictionary with one key and multiple value

Example

C#
Dictionary<int, string,string,bool,int,string......... > dict = new Dictionary<int, string,string,bool,int,string......... >();
Posted
Comments
BillWoodruff 19-Oct-15 8:47am    
if all your values were the same Type, you could a Dictionary<int, List<string>>
Philippe Mori 27-Oct-15 12:19pm    
That code is not maintanable. Uses specific objects as indicated in solutions 1and 2.

Simply group your 'multiple values' in a object and store the {key, object} pair in the standard Dictionary.
 
Share this answer
 
Comments
Maciej Los 19-Oct-15 9:22am    
5ed!
Matt T Heffron 26-Oct-15 13:12pm    
+5
You can simply create a class like:

C#
public class MultipleValues
{
    public string value1;
    public bool value2;
    public int value3;
    // add all the values you need here
}


and then you can use it in the Dictionary like:

C#
var dict = new Dictionary<int, MultipleValues>();
 
Share this answer
 
v2
Comments
Maciej Los 19-Oct-15 9:24am    
+5!
Matt T Heffron 26-Oct-15 13:12pm    
+5
another solution could be using a tuple within your dictionary

var dictionary = new Dictionary<string, Tuple<string, int>>();
 
Share this answer
 
v4
Comments
Matt T Heffron 26-Oct-15 13:11pm    
OP Seems to have a pretty long list of values.
Beyond 7 values, Tuples need to be nested which will make for some pretty ugly code.
Making an application specific class to hold exactly the correct types, and giving them meaningful names is the way to go.
El_Codero 26-Oct-15 13:23pm    
Hi Matt, yes your absolutely right...but "multiple" could also mean less than 8 values ;)... the meaningful names within a class are definitely a big plus. Regards
Seems like most of the answers overlook the obvious.

You want your key to be an int and the value to be multiple values of any type. Why not use the boxing feature of .NET?


C#
var map = new Dictionary<int, List<object> >();

List<object> list;
if ( map.TryGetValue(5, out list) )
{
    foreach (var item in list)
        Console.WriteLine(item);
}
</object>
 
Share this answer
 
v2
Comments
phil.o 26-Oct-15 6:39am    
Except boxing/unboxing operations are really costly. Moreover, it's not really easy to get back a given value in its original type when you stored all as objects. Other solutions do not have these issues.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900