首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >除了Dictionary/SortedList之外,有没有一种允许重复的方法?

除了Dictionary/SortedList之外,有没有一种允许重复的方法?
EN

Stack Overflow用户
提问于 2009-02-16 00:18:21
回答 7查看 26K关注 0票数 22

可能重复:

C# Sortable collection which allows duplicate keys

基本上,我想让Dictionary使用重复的键,而不是进入自定义的比较器实现。有一种想法是:

  Dictionary<key, List<value>>

但它仍然有一些开销。我希望字典有"AllowDuplicates“。

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2009-02-16 00:24:05

如果你使用的是.NET 3.5,那么Lookup可能就是你想要的。

票数 14
EN

Stack Overflow用户

发布于 2009-02-16 00:23:51

.NET 2.0:PowerCollections包含OrderedMultiDictionary

票数 7
EN

Stack Overflow用户

发布于 2011-10-14 23:00:16

您仍然可以使用SortedList,并尝试通过将值和Guid组合到一个类中来创建一个惟一的键。在这种情况下,您必须为新密钥实现IComparer<NewKey>,如下所示:

class MyKey
{
    public Guid Guid { get; set; }
    public float Value { get; set; }
}

class MyComparer : IComparer<MyKey>
{

    public int Compare(MyKey x, MyKey y)
    {
        if (x == null || y == null)
            throw new InvalidOperationException("both of parameters must be not null");
        if (x.Value < y.Value) return -1;
        if (x.Value > y.Value) return 1;
        return 0;
    }
}

然后

var mySortedList = new SortedList<MyKey, MyValue>(new MyComparer());
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/551901

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档