2

我需要一个类型,如 a TupleorKeyValuePair类型,并带有设置其值的选项(可变类型)。任何可以满足这些需求的现有泛型的想法?

(在我看来,字典对于我的目的来说有点矫枉过正)

我也可以编写自己的类,但我希望KeyValuePair已经实现了一些可变类型。

4

3 回答 3

2

具有可变的问题是,您可以很容易地最终引用同一个元组多次,您在一个地方更改值,它也会在您不期望它的其他地方更改。因此,如果您确实使用可变元组,则必须非常严格地在适当的位置克隆元组,例如,如果您将其放入集合中。

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

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;
    }
}
于 2013-08-17T11:54:47.287 回答
1
public class MutableKeyValuePair<TKey, TValue>
{
    public TKey Key { get; set; }
    public TValue Value { get; set; }
}

而已。

于 2013-08-17T11:50:33.040 回答
0

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

请注意,实现 aMutableTuple<T1, ...>非常容易。

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

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;
    }
}
于 2013-08-17T11:48:34.483 回答