zoukankan      html  css  js  c++  java
  • 【tag】Tuple 类 使用介绍

    官方介绍地址:

     Tuple 类

    参考文章地址:

    http://blog.csdn.net/aoshilang2249/article/details/40053213
    http://www.cnblogs.com/codelir/p/5143257.html
    https://www.oschina.net/translate/tuple-in-c-sharp-7?cmp

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace CommonUtility
    {
        /// <summary>
        /// Tuple是异类对象的有序序列。 我们经常可以写出返回多个值的方法,所以我们需要创建一个包含多个数据元素的简单结构。 
        /// 为了支持这些情况,Tuple 被添加到 C#。 Tuple 是包含多个字段用来表示数据成员的轻量级数据结构。
        /// 如果一个方法返回多个相同类型的数值,那么它可以将这些值存储在一个集合中并返回该集合。 
        /// 但是如果一个方法需要返回多个不同类型的值呢,C# 提供了一些可选项,比如 Class / Struct,输出参数和 Tuple。
        /// </summary>
        public class TupleIntroduce
        {
            /// <summary>
            /// Tuple 是异类对象的有序序列。 当一个方法需要返回多个值的时候使用它。Tuple 实例的条目数是固定的。
            /// Tuple 有最大数目为 8 项的限制。 如果我们想创建一个带有更多项的 Tuple,我们必须创建嵌套的 Tuple。 Tuple 的第八项必须是另一个 Tuple。
            /// </summary>
            private void demo()
            {
                //一个成员  
                Tuple<int> test = new Tuple<int>(1);
                Console.WriteLine(test.Item1);
    
                //两个成员  
                Tuple<int, double> test1 = new Tuple<int, double>(2, 2.3);
                Console.WriteLine(test1.Item1 + test1.Item2);
    
                //非8个元素  
                Tuple<int, Tuple<string>> test2 = new Tuple<int, Tuple<string>>(3, new Tuple<string>("Nesting"));
                Console.WriteLine(test2.Item1);
                Console.WriteLine(test2.Item2);
                Console.WriteLine(test2.Item2.Item1);
    
                //8个元素  注意第8个成员很特殊,第8个成员必须嵌套定义成Tuple类型
                Tuple<int, long, float, double, short, byte, char, Tuple<int>> test3 =
                    new Tuple<int, long, float, double, short, byte, char, Tuple<int>>(1,
                        2, 3.0f, 4, 5, 6, 'h', new Tuple<int>(8));
                Console.WriteLine(test3.Item4 + test3.Rest.Item1);
            }
    
    
            /// <summary>
            /// 元组是一种数据结构,具有特定数量和元素序列。 元组的一个示例是用于存储人员的姓名等标识符的第一个元素,第二个元素和人员收入中该年度第三个元素中的每一年中的数据结构具有三个元素 (称为 3 元组或三元组)。
            /// .NET Framework 直接支持具有 1 到 7 元素的元组。 此外,您可以创建由嵌套中的元组对象的元组的八个或多个元素Rest属性Tuple<T1, T2, T3, T4, T5, T6, T7, TRest>对象。
            /// 元组常用四种方法︰
            /// 来表示一组数据。 例如,一个元组可以表示的数据库记录,并且其组件可以表示每个字段的记录。
            /// 若要提供轻松访问和数据集的操作。
            /// 若要从方法返回多个值,而无需使用out参数 (在 C# 中) 或ByRef参数 (在 Visual Basic 中)。
            /// 若要将多个值传递给通过单个参数的方法。 例如,Thread.Start(Object)方法只有一个参数,允许你提供一个线程在启动时执行的方法的值。 
            /// 如果你提供Tuple<T1, T2, T3>对象作为方法自变量,则可以提供有三个项的数据的线程的启动例程。
            /// </summary>
            private void TupleCreate()
            {
                // 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
    
                //通过使用一个帮助器方法创建相同的元组对象是更为简单,如以下示例所示。
                // 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
    
                //Create帮助器方法直接支持创建有一至八个组件 (即,到八元组的单一实例) 的元组对象。 
                //尽管没有组件的数量没有实际限制但元组可能具有,帮助程序方法不是可用于创建具有九个或多个组件的元组。 
                //若要创建此类元组,必须调用Tuple<T1, T2, T3, T4, T5, T6, T7, TRest>.Tuple<T1, T2, T3, T4, T5, T6, T7, TRest>构造函数。
                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
            }
    
        }
    }
  • 相关阅读:
    改变对象的字符串提示
    perl 和 python中的回调函数
    shiro权限验证标签
    user_tables 的信息依赖于统计信息
    centos jdk 1.7升级到1.8后显示还是1.7
    7.1 可接受任意数量参数的函数:
    IntelliJ IDEA中怎么查看文件中所有方法(类似eclipse里面的outline)
    Intellij IDEA 代码格式化与eclipse保持风格一致
    jquery-1.9.1.min.js:69 Uncaught TypeError: Illegal invocation
    python json模块
  • 原文地址:https://www.cnblogs.com/FH-cnblogs/p/6928251.html
Copyright © 2011-2022 走看看