c#中的多键字典? [英] Multi-key dictionary in c#?

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

问题描述

我知道BCL中没有一个,但任何人都可以指向一个好的开源软件。



通过Multi我的意思是2键。 ; - )

解决方案

我已经用Jason做的元组了。然而,我建议你简单地将一个元组定义为一个结构体:

  public struct Tuple< T1,T2> {
public readonly T1 Item1;
public readonly T2 Item2;
public Tuple(T1 item1,T2 item2){Item1 = item1; Item2 = item2;}
}

public static class Tuple {// for type-in​​ference goodness。
public static Tuple< T1,T2>创建< T1,T2>(T1 item1,T2 item2){
return new Tuple }
}

您将获得不变性,。 GetHashcode .Equals 免费,哪(当你等待C#4.0)是很好'简单...



然而,一个警告:默认的 GetHashcode 实现(有时)只考虑第一个字段,所以请确保使第一个字段是最有区别或实现的 GetHashcode 例如使用 ValueUtils FieldwiseHasher.Hash(this) >),否则你可能会遇到可扩展性问题。



另外,你可以避免这些往往使事情变得复杂的null(如果你真的想要null,你只是使你的 Tuple<> 可空)。稍微偏偏,我是唯一一个在框架层面缺乏对非空引用的支持的烦恼吗?我在大型项目上工作,偶尔会在某个地方真的不应该这样做 - 而且,你可以得到一个null引用异常 - 但是使用堆栈跟踪来指向引用的第一个用法,而不是实际的错误代码。



当然,.NET 4.0现在很旧了;大多数使用可以只使用.NET 4.0的元组。



***编辑:***解决不好的 GetHashCode .NET为我提供的结构体提供了 ValueUtils ,这也可以让你为您的多字段键使用真实姓名;这意味着您可能会写下如下:

 密封类MyValueObject:ValueObject< MyValueObject> {
public DayOfWeek day;
public string NamedPart;
//属性工作正常
}

...希望能够至少在某些未来版本的C#实现之前,使用具有价值语义的数据更易于使用人类可读的名称具有命名成员的适当元组;希望使用体面的哈希码; - )。


I know there isn't one in the BCL but can anyone point me to a good opensource one?

By Multi I mean 2 keys. ;-)

解决方案

I've alse used tuples as Jason does. However, I suggest you simply define a tuple as a struct:

public struct Tuple<T1, T2> {
    public readonly T1 Item1;
    public readonly T2 Item2;
    public Tuple(T1 item1, T2 item2) { Item1 = item1; Item2 = item2;} 
}

public static class Tuple { // for type-inference goodness.
    public static Tuple<T1,T2> Create<T1,T2>(T1 item1, T2 item2) { 
        return new Tuple<T1,T2>(item1, item2); 
    }
}

You get immutability, .GetHashcode and .Equals for free, which (while you're waiting for C# 4.0) is nice 'n simple...

One warning however: the default GetHashcode implementation (sometimes) only considers the first field so make sure to make the first field the most discriminating or implement GetHashcode yourself (e.g. using FieldwiseHasher.Hash(this) from ValueUtils), otherwise you'll likely run into scalability issues.

Also, you get to avoid nulls which tend to complicate matters (and if you really want nulls, you just make your Tuple<> nullable). Slightly offtopic, am I the only one annoyed at the framework-level lack of support for non-null references? I work on large project, and occasionally a null creeps in somewhere it really shouldn't -- and hey presto, you get a nullreference exception -- but with a stack trace that points you to the reference's first usage, not the actually faulty code.

Of course, .NET 4.0 is pretty old by now; most of use can just use .NET 4.0's tuple.

***Edit:***to workaround the poor GetHashCode implementation that .NET provides for structs I've written ValueUtils, which also allows you to use real names for your multi-field keys; that means you might write something like:

sealed class MyValueObject : ValueObject<MyValueObject> {
    public DayOfWeek day;
    public string NamedPart;
    //properties work fine too
}

...which hopefully makes it easier to have human-readable names for data with value semantics, at least until some future version of C# implements proper tuples with named members; hopefully with decent hashcodes ;-).

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

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