1. 程式人生 > >【tag】Tuple 類 使用介紹

【tag】Tuple 類 使用介紹

pac sdn .net 對象 此外 執行 輸出參數 var 返回

官方介紹地址:

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 } } }

【tag】Tuple 類 使用介紹