Equivalent of Tuple (.NET 4) for .NET Framework 3.5
Solution 1:
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:
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);
}
}