.NET字典中的重复键? [英] Duplicate keys in .NET dictionaries?

查看:135
本文介绍了.NET字典中的重复键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在.NET基类库中是否有任何允许使用重复键的字典类?我找到的唯一解决方案是创建一个类,如:

Are there any dictionary classes in the .NET base class library which allow duplicate keys to be used? The only solution I've found is to create, for example, a class like:

Dictionary<string, List<object>>

但是这实际上是很刺激的。在Java中,我相信MultiMap会实现这一点,但是在.NET中找不到模拟。

But this is quite irritating to actually use. In Java, I believe a MultiMap accomplishes this, but cannot find an analog in .NET.

推荐答案

如果您使用的话。 NET 3.5,使用 查找

If you're using .NET 3.5, use the Lookup class.

编辑:您通常使用 Lookup /msdn.microsoft.com/en-us/library/system.linq.enumerable.tolookup.aspxrel =noreferrer> Enumerable.ToLookup 。这确实假定你以后不需要改变它 - 但是我通常会发现这很好。

You generally create a Lookup using Enumerable.ToLookup. This does assume that you don't need to change it afterwards - but I typically find that's good enough.

如果那个工作对于你,我不认为框架中有什么可以帮助的 - 并且使用字典与它得到的一样好:(

If that doesn't work for you, I don't think there's anything in the framework which will help - and using the dictionary is as good as it gets :(

这篇关于.NET字典中的重复键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆