7

I am using a C# dictionary.

Dictionary<int, string> d = new Dictionary<int, string>();

I am using the dictionary as then I can have the key value be unique and is enforced by the Dictionary.

The key value will be int. Before I had the need of only one value. Now things have expanded to where I need to store multiple values(with different data types) for the same key.

Something like this - (although it will not work):

Dictionary<int,string,string,bool, bool,int,bool> d = new Dictionary<int, string,string,bool, bool,int,bool>();

Note that string,string,bool, bool,int,bool are additional values I like to store for the key field which will be an int.

I am not sure how I would go about doing this. How can I get a key value to store multiple values with different data types. On Way that I see that this may be possible is to store the additional fields into a List but not sure how it will all come togehter. If somebody can provide and example that would be appreciated.

Thanks in advance.

4 Answers 4

22

Make a custom data type with fields that are string, string, bool, bool, int, bool and use this as your Dictionary value:

class MyType
{
    public string SomeVal1{ get; set; }
    public string SomeVal2{ get; set; }
    public bool SomeVal3{ get; set; }
    public bool SomeVal4{ get; set; }
    public int SomeVal5{ get; set; }
    public bool SomeVal6{ get; set; }
}

then

var someDictionary = new Dictionary<int, MyType>();

and

someDictionary.Add( 0, 
                    new MyType{
                        SomeVal1 = "foo",
                        SomeVal2 = "bar",
                        SomeVal3 = true,
                        SomeVal4 = false,
                        SomeVal5 = 42,
                        SomeVal6 = true
                    });

//someDictionary[0].SomeVal2 // bar
Sign up to request clarification or add additional context in comments.

Comments

14

You can use a Tuple as the generic type for the value.

var d = new Dictionary<int,Tuple<string,string,bool, bool,int,bool>>();

Alternatively, create a type (class or struct) that holds the different types as a group - chances are that this would be a better way to model things.

3 Comments

+1 simply because this is sexy and I've never seen it before.
I'd caution against using the tuple, because while you get strong typing, you have no context for what the values mean. In this case, I'd elevate the encapsulating class suggestion from being the alternate to being the main answer, as @spender has done.
I agree with @AnthonyPegram, please don't use a Tuple. IMO Tuples are just laziness on the part of the developer. Classes are typesafe and context clear.
0

The only think to all what geeks here said I can add, I think you should to encapsulate that dictionary into some class, so the user of that dictionary wrapper will never figure out what is happening under the hoods, and implement inside of that class your prefered recovery logic, or one of listed here.

1 Comment

I would suggest a struct over a class, as it will contain no methods and therefore, better to use a value type than reference type, which is on the heap and requires a garbage collection effort to free up the memory, after it is disposed of. I certainly wouldn't use a Tuple. In fact, I think using a Tuple is as bad a practice as using var for named types.
0

Dictionaries are key / value pairings. Where one key corresponds to one value. The value can be any object however, as such you can simple create an object that contains all the members you desire. Consider the following:

var d = new Dictionary<int, string, string, bool, bool, int, bool>();

The consumer of this dictionary will have no idea what these primitive types data is intended to represent. You should create an object:

public class Person : IPerson
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public bool IsMarried { get; set; }
    public bool HasChildren { get; set; }        
    public bool IsHappy { get; set; }

    public Person(int id)
    {
        Id = id;
    }
}

Obviously this object is entirely made up for the purpose of matching the types and representing the data in a container. With this understanding you could then do the following:

var d = new Dictionary<int, IPerson>();

3 Comments

But what if you have a Dictionary<Int64, List<Int64>> ? How do you get the values out of this type of data structure?
The values are accessible via their key, for example if you have that type of dictionary and there is a key of 7L - the corresponding list of Int64 values are available at dictionaryInstance[7L].
I have it working now, should I edit my question or something with the code I came up with?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.