.NET Framework 3.5 용 Tuple (.NET 4)에 해당
.NET 4 Tuple에 해당하는 .NET Framework 3.5에 클래스가 있습니까?
을 만드는 대신 메서드에서 여러 값을 반환하기 위해 사용하고 싶습니다 struct
.
아니요, .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;
}
}
업데이트 : 유형 추론을 허용하기 위해 정적 항목을 정적 클래스로 이동했습니다. 업데이트를 통해 다음과 같은 var tuple = Tuple.New(5, "hello");
것을 작성할 수 있으며 암시 적으로 유형을 수정합니다.
나는 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);
}
}
.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
}
NetLegacySupport.Tuple
너겟을 통해 설치할 수 있습니다 . 이것은 .Net 4.5에서 .Net 2.0 및 3.5로 백 포트 된 Tuple 클래스입니다.
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
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
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.
참고URL : https://stackoverflow.com/questions/7120845/equivalent-of-tuple-net-4-for-net-framework-3-5
'IT TIP' 카테고리의 다른 글
문제 : 치명적 오류 : [] 연산자가 문자열에 지원되지 않음 (0) | 2020.12.10 |
---|---|
Gunicorn Nginx 시간 초과 문제 (0) | 2020.12.10 |
git은 특별히 명명 된 파일 2 개를 무시하지 않습니다. (0) | 2020.12.10 |
파이썬 삼항 연산자없이 (0) | 2020.12.10 |
android getIntrinsicHeight 및 getIntrinsicWidth는 무엇을 의미합니까? (0) | 2020.12.10 |