简体   繁体   English

C# 字典,每个键有多个值

[英]C# dictionary with multiple values per key

I am using a C# dictionary.我正在使用 C# 字典。

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.键值将是 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.请注意,string、string、bool、bool、int、bool 是我喜欢为关键字段存储的附加值,它将是一个 int。

I am not sure how I would go about doing this.我不确定我会如何 go 这样做。 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:使用string, string, bool, bool, int, bool字段创建自定义数据类型,并将其用作您的Dictionary值:

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.您可以使用Tuple作为值的泛型类型。

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.或者,创建一个将不同类型作为一个组保存的类型(类或结构) - 这可能是 model 事物的更好方法。

Dictionaries are key / value pairings.字典是key / value对。 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.该值可以是任何 object,但是,您可以简单地创建一个包含您想要的所有成员的 object。 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:您应该创建一个 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.显然这个 object 完全是为了匹配类型和表示容器中的数据。 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.对这里所有极客所说的我可以添加的唯一想法,我认为您应该将该字典封装到一些 class 中,因此该字典包装器的用户永远不会弄清楚引擎盖下发生了什么,并在 class 内部实现您的首选恢复逻辑,或此处列出的逻辑之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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