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
.
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
.
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.
Tuple
class appears to have changed in .NET 4.5; the properties arecalled Item1
and Item2
instead of First
and Second
. –
Sociology 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);
}
}
Tuple<T1>
? Is that even a tuple? –
Blaze 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
}
GetHashCode()
implementation is (h1 << 3) ^ h2
while MS one (trought ILSpy) is (h1 << 5) + h1 ^ h2
. does it really make a difference (performance wise) ? –
Chifley 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:
Yes, there is a class called System.Collections.Generic.KeyValuePair that does the same thing (since .NET 2.0 I think).
KeyValuePairs
within other KeyValuePairs
for a quick fix. –
Jackscrew 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.
© 2022 - 2024 — McMap. All rights reserved.
Tuple
class for two items. – BlazeKeyValuePair
implies that there is a value associated to a key. Not what I wanted at the time. – Moirstruct
so can't be used withas
for example. – Previse