简体   繁体   English

C#字典每个键有两个值?

[英]C# Dictionary with two Values per Key?

I have a situation in code where a Dictionary<string, string> seemed like the best idea - I need a collection of these objects and I need them to be accessible via a unique key. 我有一个代码的情况,其中Dictionary<string, string>似乎是最好的主意 - 我需要这些对象的集合,我需要它们可以通过唯一的密钥访问。 Exactly what the Dictionary concept is for, right? 究竟是什么字典概念,对吧?

Well, the requirements have expanded to the point where I now need to hold an additional bit of information per-key (a boolean value, if you're curious). 好吧,这些要求已经扩展到我现在需要为每个键保存一点额外信息的点(如果你很好奇,则为布尔值)。

So, I figure expand the concept to create a new data structure with the string and the boolean and have it now be a Dictionary<string, NewCustomObject> . 所以,我想扩展概念,用字符串和布尔值创建一个新的数据结构,现在它是一个Dictionary<string, NewCustomObject>

However, for just one additional value like a boolean flag, it just feels like overkill. 但是,对于像布尔标志这样的一个额外值,它只是感觉有点过分。 And yet I don't know of any Dictionary-like generic object with two values per key. 然而,我不知道任何类似字典的通用对象,每个键有两个值。

Is just having a Dictionary of custom objects the best way to go about this or is there something simpler for this scenario? 只是有一个自定义对象字典是最好的方法,或者这个场景有什么更简单的东西吗?

Actually, what you've just described is an ideal use for the Dictionary collection. 实际上,你刚刚描述的是Dictionary集合的理想用途。 It's supposed to contain key:value pairs, regardless of the type of value. 无论值的类型如何,它都应该包含键:值对。 By making the value its own class, you'll be able to extend it easily in the future, should the need arise. 通过将值设为自己的类,如果需要,您将能够在将来轻松扩展它。

class MappedValue
{
    public string SomeString { get; set; }
    public bool SomeBool { get; set; }
}

Dictionary<string, MappedValue> myList = new Dictionary<string, MappedValue>;

I think generally you're getting into the concept of Tuples - something like Tuple<x, y, z> , or Tuple<string, bool, value> . 我认为通常你会进入元组的概念 - 像Tuple<x, y, z>Tuple<string, bool, value>

C# 4.0 will have dynamic support for tuples, but other than that, you need to roll your own or download a Tuple library. C#4.0将为元组提供动态支持,但除此之外,您需要自己动手或下载Tuple库。

You can see my answer here where I put some sample code for a generic tuple class. 您可以在此处查看我的答案,其中我为通用元组类提供了一些示例代码。 Or I can just repost it here: 或者我可以在这里重新发布:

public class Tuple<T, T2, T3>
{
    public Tuple(T first, T2 second, T3 third)

    {
        First = first;
        Second = second;
        Third = third;
    }

    public T First { get; set; }
    public T2 Second { get; set; }
    public T3 Third { get; set; }

}

在.NET4中,您可以使用(未​​选中): Dictionary<string, Tuple<bool,string>>

I don't believe .net has Multi-Map built in which is generally a data-structure you can use to do this type of storage. 我不相信.net内置了Multi-Map,它通常是一种可用于执行此类存储的数据结构。 On the other hand I don't think it's overkill at all just using a custom object that holds both a string and a boolean. 另一方面,我认为只使用一个包含字符串和布尔值的自定义对象完全是不合适的。

Doesn't this work for ya? 这不适合你吗?

Dictionary<string, List<string>>

Or you could use a Tuple and have a dictionary of that: 或者你可以使用一个元组并有一个字典:

Dictionary<string, Tuple<string, bool>>

Tuple is always a good solution. 元组总是一个很好的解决方案。 Furthermore in an object oriented approach, always favour composition over inheritance. 此外,在面向对象的方法中,总是倾向于组合而不是继承。 Construct a composite object doing the grouping. 构造执行分组的复合对象。 Simply. 只是。 I think you are covered with some nice and clean solutions here, from fellow stackoverflow'ers. 我认为你在这里有一些很好的和干净的解决方案,来自其他stackoverflow'ers。 :) :)

A Dictionary is just a keyed collection of objects. 字典只是一个关键的对象集合。 As such, it will hold any type of object you want. 因此,它将包含您想要的任何类型的对象。 Even the simplest of objects, like, say, an array (which derives from Object), but requires a lot less code on your behalf. 即使是最简单的对象,例如,一个数组 (派生自Object),但代表您需要的代码少得多。

Rather than writing an entire class, stuff a dynamically allocated array, or a List in the value and run with that. 不是编写整个类,而是在值中填充动态分配的数组或List,然后运行它。

Dictionaries are highly flexible that way. 字典在这方面非常灵活。

What about Lookup class? Lookup课怎么样?

Edit: 编辑:

After read the question carefully think this is not the best solution, but it still a good one for the dictionary with some values per a single key. 在仔细阅读之后,请仔细考虑这不是最佳解决方案,但对于字典而言,它仍然是一个很好的解决方案。

另一个想法是将布尔值存储在单独的数据结构中,例如HashSet。

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

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