需要建议像Tuple或keyValuePair这样的DataStructure但是可变类型

时间:2013-08-17 11:37:04

标签: c# generics data-structures keyvaluepair

我需要类似Tuple或KeyValuePair类型,并且可以选择设置其值(可变类型)。 现有泛型的AnyIdeas实现了这种情况吗?

(字典在我看来是某种矫枉过正的目的) 我也可以编写自己的类,但我猜有一些可变类型的keyValue已经实现。

由于

3 个答案:

答案 0 :(得分:2)

具有可变性的问题在于,您可以非常轻松地多次引用相同的元组,在一个地方更改值,它也会在您不期望的地方更改。因此,如果您使用可变的tubles,则必须非常严格地在适当的位置克隆元组,例如,如果您将它放入集合中。

但是一个可变的元组看起来像这样:

public class MutableTuple<T1, T2> {
    public T1 Item1 { get; set; }
    public T2 Item2 { get; set; }

    public MutableTuple(T1 item1, T2 item2) {
        Item1 = item1;
        Item2 = item2;
    }
}

答案 1 :(得分:1)

public class MutableKeyValuePair<TKey, TValue>
{
    public TKey Key { get; set; }
    public TValue Value { get; set; }
}

就是这样。

答案 2 :(得分:0)

不,没有。除非你认为object[]是一个: - )

请注意,实施MutableTuple<T1, ...>非常简单。

MutableTuple<T1, T2, T3>的完整实现,几乎等同于Tuple<T1, T2, T3>(各种接口是公开实现的,而不是私有的)(部分基于Tuple<>的Mono实现)

public static class MutableTuple
{
    public static MutableTuple<T1, T2, T3> Create<T1, T2, T3>(T1 item1, T2 item2, T3 item3)
    {
        return new MutableTuple<T1, T2, T3>(item1, item2, item3);
    }
}

[Serializable]
public class MutableTuple<T1, T2, T3> : IComparable, IStructuralEquatable, IStructuralComparable
{
    public T1 Item1 { get; set; }
    public T2 Item2 { get; set; }
    public T3 Item3 { get; set; }

    public MutableTuple(T1 item1, T2 item2, T3 item3)
    {
        this.Item1 = item1;
        this.Item2 = item2;
        this.Item3 = item3;
    }

    public override bool Equals(object obj)
    {
        return this.Equals(obj, EqualityComparer<object>.Default);
    }

    public override int GetHashCode()
    {
        return this.GetHashCode(EqualityComparer<object>.Default);
    }

    public override string ToString()
    {
        var sb = new StringBuilder();

        sb.Append(this.Item1);
        sb.Append(", ");
        sb.Append(this.Item2);
        sb.Append(", ");
        sb.Append(this.Item3);
        sb.Append(")");

        return sb.ToString();
    }

    public int CompareTo(object obj)
    {
        return this.CompareTo(obj, Comparer<object>.Default);
    }

    public bool Equals(object obj, IEqualityComparer comparer)
    {
        if (obj == null)
        {
            return false;
        }

        var tuple = obj as MutableTuple<T1, T2, T3>;
        return tuple != null && (comparer.Equals(this.Item1, tuple.Item1) && comparer.Equals(this.Item2, tuple.Item2)) && comparer.Equals(this.Item3, tuple.Item3);
    }

    public int GetHashCode(IEqualityComparer comparer)
    {
        unchecked
        {
            int hash = 17;

            hash = (hash * 23) + comparer.GetHashCode(this.Item1);
            hash = (hash * 23) + comparer.GetHashCode(this.Item2);
            hash = (hash * 23) + comparer.GetHashCode(this.Item3);

            return hash;
        }
    }

    public int CompareTo(object obj, IComparer comparer)
    {
        if (obj == null)
        {
            return 1;
        }

        var other = obj as MutableTuple<T1, T2, T3>;

        if (other == null)
        {
            throw new ArgumentException();
        }

        int res = comparer.Compare(this.Item1, other.Item1);

        if (res != 0)
        {
            return res;
        }

        res = comparer.Compare(this.Item2, other.Item2);

        if (res != 0)
        {
            return res;
        }

        res = comparer.Compare(this.Item3, other.Item3);

        return res;
    }
}