zoukankan      html  css  js  c++  java
  • C#元组ValueTuple

    定义:

    元组是具有特定数量和元素序列的数据结构。有几个元素称为 几元组。

    .NET Framework 直接支持包含1到7个元素的元组。 此外,通过在对象的属性中嵌套元组对象,可以创建八个或多个元素的元组 Rest Tuple<T1,T2,T3,T4,T5,T6,T7,TRest>

    常用的四种使用方式:

    • 表示单个数据集。例如表示数据库记录。
    • 提供对数据集的轻松访问和操作。
    • 如果一个方法返回多个值,不用out参数,可以用元组。
    • 向单个参数的方法传递多个值。

    System.Tuple

    C# 4.0引入的新特性,引用类型。

    创建

    尽管可以通过调用类构造函数来创建元组类的实例,但执行此操作的代码可能会很繁琐。

    // Create a 7-tuple.
    var population = new Tuple<string, int, int, int, int, int, int>(
                               "New York", 7891957, 7781984, 
                               7894862, 7071639, 7322564, 8008278);
    // Display the first and last elements.
    Console.WriteLine("Population of {0} in 2000: {1:N0}",
                      population.Item1, population.Item7);
    // The example displays the following output:
    //       Population of New York in 2000: 8,008,278
    

    使用 helper 方法创建相同的元组对象更简单

    // Create a 7-tuple.
    var population = Tuple.Create("New York", 7891957, 7781984, 7894862, 7071639, 7322564, 8008278);
    // Display the first and last elements.
    Console.WriteLine("Population of {0} in 2000: {1:N0}",
                      population.Item1, population.Item7);
    // The example displays the following output:
    //       Population of New York in 2000: 8,008,278
    

    helper方法

    Create<T1,T2,T3,T4,T5,T6,T7,T8>(T1, T2, T3, T4, T5, T6, T7, T8)
    //创建新的 8 元组,即八元组。
    Create<T1,T2,T3,T4,T5,T6,T7>(T1, T2, T3, T4, T5, T6, T7)
    //创建新的 7 元组,即七元组。
    Create<T1,T2,T3,T4,T5,T6>(T1, T2, T3, T4, T5, T6)
    //创建新的 6 元组,即六元组。
    Create<T1,T2,T3,T4,T5>(T1, T2, T3, T4, T5)
    //创建新的 5 元组,即五元组。
    Create<T1,T2,T3,T4>(T1, T2, T3, T4)
    //创建新的 4 元组,即四元组。
    Create<T1,T2,T3>(T1, T2, T3)
    //创建新的 3 元组,即三元组。
    Create<T1,T2>(T1, T2)
    //创建新的 2 元组,即二元组。
    Create<T1>(T1)
    //创建新的 1 元组,即单一实例。
    

    创建一个8元组

    var primes = Tuple.Create(2, 3, 5, 7, 11, 13, 17, 19);
    Console.WriteLine("Prime numbers less than 20: " + 
                      "{0}, {1}, {2}, {3}, {4}, {5}, {6}, and {7}",
                      primes.Item1, primes.Item2, primes.Item3, 
                      primes.Item4, primes.Item5, primes.Item6,
                      primes.Item7, primes.Rest.Item1);
    // The example displays the following output:
    //    Prime numbers less than 20: 2, 3, 5, 7, 11, 13, 17, and 19
    

    构造函数

    var primes = new Tuple<int, int, int, int, int, int, int, 
                     Tuple<int>>(2, 3, 5, 7, 11, 13, 16, 
                     new Tuple<int>(19));
    

    ValueTuple

    C#刚开始引入的元组为System.Tuple,此类型为引用类型。后来发现值类型更适合元组,所以C#7.0开始引入值类型ValueTuple来代替引用类型Tuple,ValueTuple包含Tuple所有的方法。且提供了简洁的语法,用于将多个数据元素分组成一个轻型数据结构。

    简洁语法

    (double, int) t1 = (4.5, 3);//t1.Item1,t1.Item2
    //具名
    (double Sum, int Count) t2 = (4.5, 3);//t2.Count,t2.Sum
    //任意数量的元素
    var t=(1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11, 12, 13, 14, 15, 16, 17, 18,19, 20, 21, 22, 23, 24, 25, 26);
    //表达式指定名称
    var t = (Sum: 4.5, Count: 3);
    //类型中指定名称
    (double Sum, int Count) d = (4.5, 3);
    //推断名称
    var sum = 4.5;
    var count = 3;
    var t = (sum, count);
    
    

    元组字段的默认名称为 Item1、Item2、Item3 等。 始终可以使用字段的默认名称,即使字段名称是显式指定的或推断出的。

    在编译时,编译器会将非默认字段名称替换为相应的默认名称。 因此,显式指定或推断的字段名称在运行时不可用。

    元组赋值

    C# 支持满足以下两个条件的元组类型之间的赋值:

    • 两个元组类型有相同数量的元素
    • 对于每个元组位置,右侧元组元素的类型与左侧相应的元组元素的类型相同或可以隐式转换为左侧相应的元组元素的类型
    (int, double) t1 = (17, 3.14);
    (double First, double Second) t2 = (0.0, 1.0);
    t2 = t1;
    Console.WriteLine($"{nameof(t2)}: {t2.First} and {t2.Second}");
    // Output:
    // t2: 17 and 3.14
    
    (double A, double B) t3 = (2.0, 3.0);
    t3 = t2;
    Console.WriteLine($"{nameof(t3)}: {t3.A} and {t3.B}");
    // Output:
    // t3: 17 and 3.14
    

    ValueTuple与System.Tuple区别

    • ValueTuple 类型是值类型。 Tuple 类型是引用类型。
    • ValueTuple 类型是可变的。 Tuple 类型是不可变的。
    • ValueTuple 类型的数据成员是字段。 Tuple 类型的数据成员是属性。

    ValueTuple数据成员public field,可以重新赋值。Tuple数据成员为只读property,只能获取,不能改变。

  • 相关阅读:
    wait
    iOS UITableviewCell优化
    iOS本地版本和服务器对比
    iOS 二维码生成 改变颜色 添加中心图
    iOS坑点解析
    iOS View快照,View截屏
    双缓冲读感感悟
    查找附近点--Geohash方案讨论
    各种报告word模板
    跳转到设置里面各个页面iOS8
  • 原文地址:https://www.cnblogs.com/wangyfb/p/15065941.html
Copyright © 2011-2022 走看看