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

问题描述

我将如何建立在C#中的多值字典?

How would i create a multi value Dictionary in c#?

例如。 词典< T,T,T> 其中第一个T是关键,另两个是值

E.g. Dictionary<T,T,T> where the first T is the key and other two are values.

所以这将是可能的:词典&LT; INT,对象,双&GT;

感谢

推荐答案

只需创建一个对&LT; TFirst,TSecond方式&gt; 键入并使用它作为你的价值。

Just create a Pair<TFirst, TSecond> type and use that as your value.

我在我的的一个例子。转载这里为简便起见:

I have an example of one in my C# in Depth source code. Reproduced here for simplicity:

using System;
using System.Collections.Generic;

public sealed class Pair<TFirst, TSecond>
    : IEquatable<Pair<TFirst, TSecond>>
{
    private readonly TFirst first;
    private readonly TSecond second;

    public Pair(TFirst first, TSecond second)
    {
        this.first = first;
        this.second = second;
    }

    public TFirst First
    {
        get { return first; }
    }

    public TSecond Second
    {
        get { return second; }
    }

    public bool Equals(Pair<TFirst, TSecond> other)
    {
        if (other == null)
        {
            return false;
        }
        return EqualityComparer<TFirst>.Default.Equals(this.First, other.First) &&
               EqualityComparer<TSecond>.Default.Equals(this.Second, other.Second);
    }

    public override bool Equals(object o)
    {
        return Equals(o as Pair<TFirst, TSecond>);
    }

    public override int GetHashCode()
    {
        return EqualityComparer<TFirst>.Default.GetHashCode(first) * 37 +
               EqualityComparer<TSecond>.Default.GetHashCode(second);
    }
}

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

11-01 13:16