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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1500835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:12:14  来源:igfitidea点击:

C# Dictionary with two Values per Key?

c#genericsdictionary

提问by Tom Kidd

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. Exactly what the Dictionary concept is for, right?

我在代码中遇到一种情况,其中 aDictionary<string, string>似乎是最好的主意 - 我需要这些对象的集合,并且需要通过唯一键访问它们。字典的概念到底是干什么用的,对吧?

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?

只是拥有一个自定义对象字典是解决这个问题的最佳方法,还是有一些更简单的方法可以解决这个问题?

采纳答案by Traveling Tech Guy

Actually, what you've just described is an ideal use for the Dictionary collection. 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.

实际上,您刚刚描述的内容是 Dictionary 集合的理想用途。无论值的类型如何,它都应该包含键:值对。通过将值作为自己的类,您将能够在未来轻松扩展它,如果需要的话。

回答by Mehrdad Afshari

Doesn't this work for ya?

这不适合你吗?

Dictionary<string, List<string>>

Or you could use a Tupleand have a dictionary of that:

或者你可以使用一个元组并有一个字典:

Dictionary<string, Tuple<string, bool>>

回答by Ralph Caraveo

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. 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.

我不相信 .net 内置了 Multi-Map,它通常是您可以用来进行此类存储的数据结构。另一方面,我认为仅使用一个同时包含字符串和布尔值的自定义对象并不过分。

回答by womp

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 将动态支持元组,但除此之外,您需要自己制作或下载元组库。

You can see my answer herewhere 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; }

}

回答by Henk Holterman

In .NET4, you could use (unchecked): Dictionary<string, Tuple<bool,string>>

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

回答by Mike Hofer

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.

字典在这种方式下非常灵活。

回答by Kamarey

What about Lookupclass?

怎么样查找类?

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.

仔细阅读问题后认为这不是最好的解决方案,但它仍然是一个很好的字典,每个键都有一些值。

回答by Ed S.

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

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

回答by reinierpost

Another idea is to store the boolean in a separate data structure, e.g. a HashSet.

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

回答by Aggelos Biboudis

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。:)