摄影瀑布流网站模板,哪个素材网站做美工最好,家庭装修报价明细预算表,会员制营销的优缺点C# 7.0已经出来一段时间了#xff0c;大家都知道新特性里面有个对元组的优化#xff1a;ValueTuple。这里利用详尽的例子详解Tuple VS ValueTuple#xff08;元组类VS值元组#xff09;#xff0c;10分钟让你更了解ValueTuple的好处和用法。
如果您对Tuple足够了解#…C# 7.0已经出来一段时间了大家都知道新特性里面有个对元组的优化ValueTuple。这里利用详尽的例子详解Tuple VS ValueTuple元组类VS值元组10分钟让你更了解ValueTuple的好处和用法。
如果您对Tuple足够了解可以直接跳过章节”回顾Tuple”直达章节”ValueTuple详解”查看值元组的炫丽用法。
回顾Tuple
Tuple是C# 4.0时出的新特性.Net Framework 4.0以上版本可用。
元组是一种数据结构具有特定数量和元素序列。比如设计一个三元组数据结构用于存储学生信息一共包含三个元素第一个是名字第二个是年龄第三个是身高。
元组的具体使用如下
1. 如何创建元组
默认情况.Net Framework元组仅支持1到7个元组元素如果有8个元素或者更多需要使用Tuple的嵌套和Rest属性去实现。另外Tuple类提供创造元组对象的静态方法。
利用构造函数创建元组
var testTuple6 new Tupleint, int, int, int, int, int(1, 2, 3, 4, 5, 6);
Console.WriteLine($Item 1: {testTuple6.Item1}, Item 6: {testTuple6.Item6});var testTuple10 new Tupleint, int, int, int, int, int, int, Tupleint, int, int(1, 2, 3, 4, 5, 6, 7, new Tupleint, int, int(8, 9, 10));
Console.WriteLine($Item 1: {testTuple10.Item1}, Item 10: {testTuple10.Rest.Item3});
利用Tuple静态方法构建元组最多支持八个元素
var testTuple6 Tuple.Createint, int, int, int, int, int(1, 2, 3, 4, 5, 6);
Console.WriteLine($Item 1: {testTuple6.Item1}, Item 6: {testTuple6.Item6});var testTuple8 Tuple.Createint, int, int, int, int, int, int, int(1, 2, 3, 4, 5, 6, 7, 8);
Console.WriteLine($Item 1: {testTuple8.Item1}, Item 8: {testTuple8.Rest.Item1});
Note这里构建出来的Tuple类型其实是Tupleint, int, int, int, int, int, int, Tupleint因此testTuple8.Rest取到的数据类型是Tupleint因此要想获取准确值需要取Item1属性。
2. 表示一组数据
如下创建一个元组表示一个学生的三个信息名字、年龄和身高而不用单独额外创建一个类。
var studentInfo Tuple.Createstring, int, uint(Bob, 28, 175);
Console.WriteLine($Student Information: Name [{studentInfo.Item1}], Age [{studentInfo.Item2}], Height [{studentInfo.Item3}]);
3. 从方法返回多个值
当一个函数需要返回多个值的时候一般情况下可以使用out参数这里可以用元组代替out实现返回多个值。
static Tuplestring, int, uint GetStudentInfo(string name)
{ return new Tuplestring, int, uint(Bob, 28, 175);
}static void RunTest()
{ var studentInfo GetStudentInfo(Bob);Console.WriteLine($Student Information: Name [{studentInfo.Item1}], Age [{studentInfo.Item2}], Height [{studentInfo.Item3}]);
} 4. 用于单参数方法的多值传递
当函数参数仅是一个Object类型时可以使用元组实现传递多个参数值。
static void WriteStudentInfo(Object student)
{ var studentInfo student as Tuplestring, int, uint;Console.WriteLine($Student Information: Name [{studentInfo.Item1}], Age [{studentInfo.Item2}], Height [{studentInfo.Item3}]);
}static void RunTest()
{ var t new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(WriteStudentInfo));t.Start(new Tuplestring, int, uint(Bob, 28, 175)); while (t.IsAlive){System.Threading.Thread.Sleep(50);}
} 尽管元组有上述方便使用的方法但是它也有明显的不足
访问元素的时候只能通过ItemX去访问使用前需要明确元素顺序属性名字没有实际意义不方便记忆最多有八个元素要想更多只能通过最后一个元素进行嵌套扩展Tuple是一个引用类型不像其它的简单类型一样是值类型它在堆上分配空间在CPU密集操作时可能有太多的创建和分配工作。
因此在C# 7.0中引入了一个新的ValueTuple类型详见下面章节。
ValueTuple详解
ValueTuple是C# 7.0的新特性之一.Net Framework 4.7以上版本可用。
值元组也是一种数据结构用于表示特定数量和元素序列但是是和元组类不一样的主要区别如下
值元组是结构是值类型不是类而元组Tuple是类引用类型值元组元素是可变的不是只读的也就是说可以改变值元组中的元素值值元组的数据成员是字段不是属性。
值元组的具体使用如下
1. 如何创建值元组
和元组类一样.Net Framework值元组也只支持1到7个元组元素如果有8个元素或者更多需要使用值元组的嵌套和Rest属性去实现。另外ValueTuple类可以提供创造值元组对象的静态方法。
利用构造函数创建元组
var testTuple6 new ValueTupleint, int, int, int, int, int(1, 2, 3, 4, 5, 6);
Console.WriteLine($Item 1: {testTuple6.Item1}, Item 6: {testTuple6.Item6}); var testTuple10 new ValueTupleint, int, int, int, int, int, int, ValueTupleint, int, int(1, 2, 3, 4, 5, 6, 7, new ValueTuple int, int, int(8, 9, 10));
Console.WriteLine($Item 1: {testTuple10.Item1}, Item 10: {testTuple10.Rest.Item3});
利用Tuple静态方法构建元组最多支持八个元素
var testTuple6 ValueTuple.Createint, int, int, int, int, int(1, 2, 3, 4, 5, 6);
Console.WriteLine($Item 1: {testTuple6.Item1}, Item 6: {testTuple6.Item6}); var testTuple8 ValueTuple.Createint, int, int, int, int, int, int, int(1, 2, 3, 4, 5, 6, 7, 8);
Console.WriteLine($Item 1: {testTuple8.Item1}, Item 8: {testTuple8.Rest.Item1});
注意这里构建出来的Tuple类型其实是Tupleint, int, int, int, int, int, int, Tupleint因此testTuple8.Rest取到的数据类型是Tupleint因此要想获取准确值需要取Item1属性。
优化区别当构造出超过7个元素以上的值元组后可以使用接下来的ItemX进行访问嵌套元组中的值对于上面的例子要访问第十个元素既可以通过testTuple10.Rest.Item3访问也可以通过testTuple10.Item10来访问。
var testTuple10 new ValueTupleint, int, int, int, int, int, int, ValueTupleint, int, int(1, 2, 3, 4, 5, 6, 7, new ValueTupleint, int, int(8, 9, 10));
Console.WriteLine($Item 10: {testTuple10.Rest.Item3}, Item 10: {testTuple10.Item10});
2. 表示一组数据
如下创建一个值元组表示一个学生的三个信息名字、年龄和身高而不用单独额外创建一个类。
var studentInfo ValueTuple.Createstring, int, uint(Bob, 28, 175);
Console.WriteLine($Student Information: Name [{studentInfo.Item1}], Age [{studentInfo.Item2}], Height [{studentInfo.Item3}]);
3. 从方法返回多个值
值元组也可以在函数定义中代替out参数返回多个值。
static ValueTuplestring, int, uint GetStudentInfo(string name)
{ return new ValueTuple string, int, uint(Bob, 28, 175);
}static void RunTest()
{ var studentInfo GetStudentInfo(Bob);Console.WriteLine($Student Information: Name [{studentInfo.Item1}], Age [{studentInfo.Item2}], Height [{studentInfo.Item3}]);
}
优化区别返回值可以不明显指定ValueTuple使用新语法(,,)代替如(string, int, uint)
static (string, int, uint) GetStudentInfo1(string name)
{ return (Bob, 28, 175);
}static void RunTest1()
{ var studentInfo GetStudentInfo1(Bob);Console.WriteLine($Student Information: Name [{studentInfo.Item1}], Age [{studentInfo.Item2}], Height [{studentInfo.Item3}]);
}
调试查看studentInfo的类型就是ValueType三元组。
优化区别返回值可以指定元素名字方便理解记忆赋值和访问
static (string name, int age, uint height) GetStudentInfo1(string name)
{ return (Bob, 28, 175);
}static void RunTest1()
{ var studentInfo GetStudentInfo1(Bob);Console.WriteLine($Student Information: Name [{studentInfo.name}], Age [{studentInfo.age}], Height [{studentInfo.height}]);
} 方便记忆赋值 方便访问 4. 用于单参数方法的多值传递
当函数参数仅是一个Object类型时可以使用值元组实现传递多个值。
static void WriteStudentInfo(Object student)
{ var studentInfo (ValueTuplestring, int, uint)student;Console.WriteLine($Student Information: Name [{studentInfo.Item1}], Age [{studentInfo.Item2}], Height [{studentInfo.Item3}]);
}static void RunTest()
{ var t new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(WriteStudentInfo));t.Start(new ValueTuplestring, int, uint(Bob, 28, 175)); while (t.IsAlive){System.Threading.Thread.Sleep(50);}
} 5. 解构ValueTuple
可以通过var (x, y)或者(var x, var y)来解析值元组元素构造局部变量同时可以使用符号”_”来忽略不需要的元素。
static (string name, int age, uint height) GetStudentInfo1(string name)
{ return (Bob, 28, 175);
}static void RunTest1()
{ var (name, age, height) GetStudentInfo1(Bob);Console.WriteLine($Student Information: Name [{name}], Age [{age}], Height [{height}]);(var name1, var age1, var height1) GetStudentInfo1(Bob);Console.WriteLine($Student Information: Name [{name1}], Age [{age1}], Height [{height1}]); var (_, age2, _) GetStudentInfo1(Bob);Console.WriteLine($Student Information: Age [{age2}]);
} 由上所述ValueTuple使C#变得更简单易用。较Tuple相比主要好处如下
ValueTuple支持函数返回值新语法”(,,)”使代码更简单能够给元素命名方便使用和记忆这里需要注意虽然命名了但是实际上value tuple没有定义这样名字的属性或者字段真正的名字仍然是ItemX所有的元素名字都只是设计和编译时用的不是运行时用的因此注意对该类型的序列化和反序列化操作可以使用解构方法更方便地使用部分或全部元组的元素值元组是值类型使用起来比引用类型的元组效率高并且值元组是有比较方法的可以用于比较是否相等详见https://msdn.microsoft.com/en-us/library/system.valuetuple。
原文地址http://www.cnblogs.com/lavender000/p/6916157.html .NET社区新闻深度好文微信中搜索dotNET跨平台或扫描二维码关注