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

C#.Net 3.5Tuples

C# Problem Overview


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, rather than create a struct.

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

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); 
    }  
}

Solution 3 - C#

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
}

Solution 4 - C#

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

Solution 5 - C#

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

Solution 6 - C#

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOtielView Question on Stackoverflow
Solution 1 - C#Tomas JanssonView Answer on Stackoverflow
Solution 2 - C#Hrvoje HudoView Answer on Stackoverflow
Solution 3 - C#Jonathan DickinsonView Answer on Stackoverflow
Solution 4 - C#Ashley DavisView Answer on Stackoverflow
Solution 5 - C#Ricky HelgessonView Answer on Stackoverflow
Solution 6 - C#Stefan SteigerView Answer on Stackoverflow