简体   繁体   中英

C# dictionary with multiple values per key

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.

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

You can use aTuple 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.

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>();

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM