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?
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");
Cheers
精彩评论