高版本的.Net 提供了Tuple 这个的只读类, 2.0是没有的.
有时候在函数需要多返回值时,要么使用 out参数,要么需要新定义一个类,很不方便.
这时候,下面的代码就可以排上用场了
#region 只读类
public class Tuple<T1>
{
private readonly T1 _item1;
public T1 Item1
{
get { return _item1; }
}
public Tuple(T1 item)
{
_item1 = item;
}
}
public class Tuple<T1,T2>:Tuple<T1>
{
private readonly T2 _item2;
public T2 Item2
{
get { return _item2; }
}
public Tuple(T1 item,T2 item2):base(item)
{
_item2 = item2;
}
}
public class Tuple<T1, T2,T3> : Tuple<T1,T2>
{
private readonly T3 _item2;
public T3 Item3
{
get { return _item2; }
}
public Tuple(T1 item, T2 item2,T3 item3) : base(item,item2)
{
_item2 = item3;
}
}
public class Tuple<T1, T2, T3,T4> : Tuple<T1, T2,T3>
{
private readonly T4 _item2;
public T4 Item4
{
get { return _item2; }
}
public Tuple(T1 item, T2 item2, T3 item3, T4 item4) : base(item, item2, item3)
{
_item2 = item4;
}
}
#endregion