相当于.NET Framework 3.5的Tuple(.NET 4)

时间:2011-08-19 11:24:29

标签: c# .net-3.5 tuples

.NET Framework 3.5中是否存在与.NET 4 Tuple等效的类?

我想使用它来从方法中返回多个值,而不是创建struct

6 个答案:

答案 0 :(得分:77)

不,不在.Net 3.5中。但创建自己的应该不难。

public class Tuple<T1, T2>
{
    public T1 First { get; private set; }
    public T2 Second { get; private set; }
    internal Tuple(T1 first, T2 second)
    {
        First = first;
        Second = second;
    }
}

public static class Tuple
{
    public static Tuple<T1, T2> New<T1, T2>(T1 first, T2 second)
    {
        var tuple = new Tuple<T1, T2>(first, second);
        return tuple;
    }
}

UPDATE:将静态内容移动到静态类以允许类型推断。通过更新,您可以编写var tuple = Tuple.New(5, "hello");之类的内容,它会隐式修复类型。

答案 1 :(得分:25)

我在我的前4个项目中使用它:

public class Tuple<T1>  
{ 
    public Tuple(T1 item1) 
    { 
        Item1 = item1; 
    }   

    public T1 Item1 { get; set; }  
} 

public class Tuple<T1, T2> : Tuple<T1>  
{ 
    public Tuple(T1 item1, T2 item2) : base(item1) 
    { 
        Item2 = item2; 
    } 

    public T2 Item2 { get; set; }  
} 

public class Tuple<T1, T2, T3> : Tuple<T1, T2>  
{ 
    public Tuple(T1 item1, T2 item2, T3 item3) : base(item1, item2) 
    { 
        Item3 = item3; 
    } 

    public T3 Item3 { get; set; }  
} 

public static class Tuple  
{ 
    public static Tuple<T1> Create<T1>(T1 item1) 
    { 
        return new Tuple<T1>(item1); 
    } 

    public static Tuple<T1, T2> Create<T1, T2>(T1 item1, T2 item2) 
    { 
        return new Tuple<T1, T2>(item1, item2); 
    } 

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

答案 2 :(得分:24)

如果您需要它们与.Net 4.0(主要是比较算法)进行功能奇偶校验:

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

[DebuggerDisplay("Item1={Item1};Item2={Item2}")]
class Tuple<T1, T2> : IFormattable
{
    public T1 Item1 { get; private set; }
    public T2 Item2 { get; private set; }

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

    #region Optional - If you need to use in dictionaries or check equality
    private static readonly IEqualityComparer<T1> Item1Comparer = EqualityComparer<T1>.Default;
    private static readonly IEqualityComparer<T2> Item2Comparer = EqualityComparer<T2>.Default;

    public override int GetHashCode()
    {
        var hc = 0;
        if (!object.ReferenceEquals(Item1, null))
            hc = Item1Comparer.GetHashCode(Item1);
        if (!object.ReferenceEquals(Item2, null))
            hc = (hc << 3) ^ Item2Comparer.GetHashCode(Item2);
        return hc;
    }
    public override bool Equals(object obj)
    {
        var other = obj as Tuple<T1, T2>;
        if (object.ReferenceEquals(other, null))
            return false;
        else
            return Item1Comparer.Equals(Item1, other.Item1) && Item2Comparer.Equals(Item2, other.Item2);
    }
    #endregion

    #region Optional - If you need to do string-based formatting
    public override string ToString() { return ToString(null, CultureInfo.CurrentCulture); }
    public string ToString(string format, IFormatProvider formatProvider)
    {
        return string.Format(formatProvider, format ?? "{0},{1}", Item1, Item2);
    }
    #endregion
}

答案 3 :(得分:5)

您可以通过nuget安装NetLegacySupport.Tuple。这是.Net 4.5的Tuple类,后向移植到.Net 2.0和3.5。

您可以通过Visual Studio中的包管理器或在命令行上使用nuget来安装它。

这是nuget包:

https://www.nuget.org/packages/NetLegacySupport.Tuple

答案 4 :(得分:2)

是的,有一个叫做System.Collections.Generic.KeyValuePair的类做同样的事情(我认为是.NET 2.0)。

http://msdn.microsoft.com/en-us/library/5tbh8a42.aspx

答案 5 :(得分:2)

是的,你可以使用mono的Tuple.cs

您还需要依赖项:
Tuples.cs
IStructuralComparable.cs
IStructuralEquatable.cs

你刚才放了一个

#define NET_4_0

在每个

前面
#if NET_4_0

然后你去了,一个功能完整的System.Tuple for .NET 2.0实现。