C# 7.0 - Deconstruction

C# 7.0 新增 Deconstruction,可將 Tuple、結構、類別的成員拆解使用。


以 Tuple 為例,若想要將 Tuple 值拆解使用,可以用小括弧宣告出多個區域變數,並將 Value Tuple 指派過去,Value Tuple 的屬性值就會依序塞入這些區域變數。

1
2
(var v1, var v2) = GetTuple();
var (v1, v2) = GetTuple();




若是結構或是類別,則要建一個 public 的 Deconstruct 方法,方法的參數用 out 將拆解出來的值依序傳出,編譯時編譯器就會自行幫我們調用 Deconstruct 方法將值拆解。

1
2
3
4
5
6
7
8
9
10
11
(var v1, var v2) = new MyClass();
...
class MyClass()
{
...
public void Deconstruct(out string v1, out string v2)
{
v1 = this.V1;
v2 = this.V2;
}
}



若有需要 Deconstruct 也支援多載。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
(var v1, var v2) = new MyClass();
...
class MyClass()
{
...
public void Deconstruct(out string v1, out string v2)
{
v1 = this.V1;
v2 = this.V2;
}

public void Deconstruct(out string v1, out string v2, out string v3)
{
v1 = this.V1;
v2 = this.V2;
v3 = this.V3;
}
}



Link