Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# dictionary with multiple values per key

Tags:

c#

.net

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.

like image 328
Nate Avatar asked Jul 15 '11 15:07

Nate


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C full form?

Full form of C is “COMPILE”. One thing which was missing in C language was further added to C++ that is 'the concept of CLASSES'.

What language is C in?

C is a procedural language that provides no support for objects and classes. C++ is a combination of OOP and procedural programming languages. C has 32 keywords and C++ has 63 keywords. C supports built-in data types, while C++ supports both built-in and user-defined data types.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

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
like image 75
spender Avatar answered Sep 30 '22 01:09

spender