替代元组 [英] Alternative to Tuples
本文介绍了替代元组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
由于.NET框架4.0支持元组。元组类是不具备的3.5,但有什么办法,我可以创建.NET 3.5我自己的类MyTuple?
As .net framework 4.0 supports Tuples. Tuple Class is not available in 3.5 But Is there any way i can create my own class MyTuple in .net 3.5?
推荐答案
是的。你可以创建自己的元组。有没有什么难了。下面是一个例子一个元组LT; T1,T2>
和元组LT; T1,T2,T3方式>
Yes. You can create your own tuples. There isn't anything hard about it. Here's an example with a Tuple<T1, T2>
and Tuple<T1, T2, T3>
.
public class Tuple<T1, T2>
{
public Tuple(T1 item1, T2 item2)
{
this.Item1 = item1;
this.Item2 = item2;
}
public T1 Item1 { get; private set; }
public T2 Item2 { get; private set; }
}
public class Tuple<T1, T2, T3>
{
public Tuple(T1 item1, T2 item2, T3 item3)
{
this.Item1 = item1;
this.Item2 = item2;
this.Item3 = item3;
}
public T1 Item1 { get; private set; }
public T2 Item2 { get; private set; }
public T3 Item3 { get; private set; }
}
这是一个用于创建元组
情况下,静态工厂类:
And here is the static factory class for creating Tuple
instances:
public static class Tuple
{
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);
}
}
现在你可以写以下内容:
Now you can write the following:
var myTuple = Tuple.Create(4, "oh yes baby");
干杯
这篇关于替代元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文