1. 程式人生 > 實用技巧 >C#(99):C# 7.0 新特性Tuple元組 、 ValueTuple 值元組詳解

C#(99):C# 7.0 新特性Tuple元組 、 ValueTuple 值元組詳解

一、Tuple元組

Tuple是C# 4.0時出的新特性,.Net Framework 4.0以上版本可用。

元組是一種資料結構,具有特定數量和元素序列,與陣列不同,元祖中的元素可以不同的資料型別。比如設計一個三元組資料結構用於儲存學生資訊,一共包含三個元素,第一個是名字,第二個是年齡,第三個是身高。

元組的具體使用如下:

1、如何建立元組

預設情況.Net Framework元組僅支援1到7個元組元素,如果有8個元素或者更多,需要使用Tuple的巢狀和Rest屬性去實現。另外Tuple類提供創造元組物件的靜態方法。

  • 利用建構函式建立元組:
var testTuple7 = new
Tuple<int, int, int, int, int, int, int>(1, 2, 3, 4, 5, 6,7); Console.WriteLine($"Item 1: {testTuple7.Item1}, Item 7: {testTuple7.Item7}"); var testTuple10 = new Tuple<int, int, int, int, int, int, int, Tuple<int, int, int>> (1, 2, 3, 4, 5, 6, 7
, new Tuple<int, int, int>(8, 9, 10)); Console.WriteLine($"Item 1: {testTuple10.Item1}, Item 10: {testTuple10.Rest.Item3}");
  • 利用Tuple靜態方法構建元組,最多支援八個元素:
var testTuple7 = System.Tuple.Create<int, int, int, int, int, int, int>(1, 2, 3, 4, 5, 6, 7);
Console.WriteLine($
"Item 1: {testTuple7.Item1}, Item 7: {testTuple7.Item7}"); var testTuple8 = System.Tuple.Create<int, 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型別其實是Tuple<int, int, int, int, int, int, int, Tuple<int>>,因此testTuple8.Rest取到的資料型別是Tuple<int>,因此要想獲取準確值需要取Item1屬性。

2、表示一組資料

如下建立一個元組表示一個學生的三個資訊:名字、年齡和身高,而不用單獨額外建立一個類。

var studentInfo = Tuple.Create<string, int, uint>("Bob", 28, 175);
Console.WriteLine($"Student Information: Name [{studentInfo.Item1}], Age [{studentInfo.Item2}], Height [{studentInfo.Item3}]");

3、從方法返回多個值

當一個函式需要返回多個值的時候,一般情況下可以使用out引數,這裡可以用元組代替out實現返回多個值。

void Main()
{
    var studentInfo = GetStudentInfo("Bob");
    Console.WriteLine($"Student Information: Name [{studentInfo.Item1}], Age [{studentInfo.Item2}], Height [{studentInfo.Item3}]");
}
static dynamic GetStudentInfo(string name)
{
    return new Tuple<string, int, uint>("Bob", 28, 175);
}

4、用於單引數方法的多值傳遞

當函式引數僅是一個Object型別時,可以使用元組實現傳遞多個引數值。

void Main()
{
    var t = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(WriteStudentInfo));
    t.Start(new Tuple<string, int, uint>("Bob", 28, 175));
    while (t.IsAlive)
    {
        System.Threading.Thread.Sleep(50);
    }
}
static void WriteStudentInfo(Object student)
{
    var studentInfo = student as Tuple<string, int, uint>;
    Console.WriteLine($"Student Information: Name [{studentInfo.Item1}], Age [{studentInfo.Item2}], Height [{studentInfo.Item3}]");
}

儘管元組有上述方便使用的方法,但是它也有明顯的不足:

  • 訪問元素的時候只能通過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 ValueTuple<int, int, int, int, int, int>(1, 2, 3, 4, 5, 6);
Console.WriteLine($"Item 1: {testTuple6.Item1}, Item 6: {testTuple6.Item6}"); 

var testTuple10 = new ValueTuple<int, int, int, int, int, int, int, ValueTuple<int, 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.Create<int, int, int, int, int, int>(1, 2, 3, 4, 5, 6);
Console.WriteLine($"Item 1: {testTuple6.Item1}, Item 6: {testTuple6.Item6}"); 

var testTuple8 = ValueTuple.Create<int, 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型別其實是Tuple<int, int, int, int, int, int, int, Tuple<int>>,因此testTuple8.Rest取到的資料型別是Tuple<int>,因此要想獲取準確值需要取Item1屬性。

優化區別:當構造出超過7個元素以上的值元組後,可以使用接下來的ItemX進行訪問巢狀元組中的值,對於上面的例子,要訪問第十個元素,既可以通過testTuple10.Rest.Item3訪問,也可以通過testTuple10.Item10來訪問。

var testTuple10 = new ValueTuple<int, int, int, int, int, int, int, ValueTuple<int, int, int>>(1, 2, 3, 4, 5, 6, 7, new ValueTuple<int, int, int>(8, 9, 10));
Console.WriteLine($"Item 10: {testTuple10.Rest.Item3}, Item 10: {testTuple10.Item10}");

2、表示一組資料

如下建立一個值元組表示一個學生的三個資訊:名字、年齡和身高,而不用單獨額外建立一個類。

var studentInfo = ValueTuple.Create<string, int, uint>("Bob", 28, 175);
Console.WriteLine($"Student Information: Name [{studentInfo.Item1}], Age [{studentInfo.Item2}], Height [{studentInfo.Item3}]");

3、從方法返回多個值

值元組也可以在函式定義中代替out引數返回多個值。

static ValueTuple<string, int, uint> GetStudentInfo(string name)
{
    return new ValueTuple<string, int, uint>("Bob", 28, 175);
}

static void Main()
{
    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 Main()
{
    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 Main()
{
    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 = (ValueTuple<string, int, uint>)student;
    Console.WriteLine($"Student Information: Name [{studentInfo.Item1}], Age [{studentInfo.Item2}], Height [{studentInfo.Item3}]");
}

static void Main()
{
    var t = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(WriteStudentInfo));
    t.Start(new ValueTuple<string, 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 Main()
{
    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