Equivalent of Tuple (.NET 4) for .NET Framework 3.5

后端 未结 6 1853
我在风中等你
我在风中等你 2020-11-29 04:05

Is there a class existing in .NET Framework 3.5 that would be equivalent to the .NET 4 Tuple?

I would like to use it in order to return several values from a method,

相关标签:
6条回答
  • 2020-11-29 04:49

    In the event that you need them to have feature-parity with .Net 4.0 (primarily comparisson):

    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
    }
    
    0 讨论(0)
  • 2020-11-29 04:53

    Yes, you can just use Tuple.cs from mono:

    You require the dependencies as well:
    Tuples.cs
    IStructuralComparable.cs
    IStructuralEquatable.cs

    You just put a

    #define NET_4_0
    

    in front of every

    #if NET_4_0
    

    and there you go, a feature-complete implementation of System.Tuple for .NET 2.0.

    0 讨论(0)
  • 2020-11-29 04:56

    No, not in .Net 3.5. But it shouldn't be that hard to create your own.

    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: Moved the static stuff to a static class to allow for type inference. With the update you can write stuff like var tuple = Tuple.New(5, "hello"); and it will fix the types for you implicitly.

    0 讨论(0)
  • 2020-11-29 05:00

    You can install NetLegacySupport.Tuple via nuget. This is the Tuple class from .Net 4.5 backported to .Net 2.0 and 3.5.

    You can install this via the package manager in Visual Studio or using nuget on the commandline.

    Here is the nuget package:

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

    0 讨论(0)
  • 2020-11-29 05:01

    I'm using this in my pre-4 projects:

    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); 
        }  
    }
    
    0 讨论(0)
  • 2020-11-29 05:07

    Yes, there is a class called System.Collections.Generic.KeyValuePair that does the same thing (since .NET 2.0 I think).

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

    0 讨论(0)
提交回复
热议问题