详解C# Tuple VS ValueTuple(元组类 VS 值元组)
C# 7.0已經(jīng)出來(lái)一段時(shí)間了,大家都知道新特性里面有個(gè)對(duì)元組的優(yōu)化:ValueTuple。這里利用詳盡的例子詳解Tuple VS ValueTuple(元組類VS值元組),10分鐘讓你更了解ValueTuple的好處和用法。
如果您對(duì)Tuple足夠了解,可以直接跳過(guò)章節(jié)”回顧Tuple”,直達(dá)章節(jié)”ValueTuple詳解”,查看值元組的炫麗用法。
回顧Tuple
Tuple是C# 4.0時(shí)出的新特性,.Net Framework 4.0以上版本可用。
元組是一種數(shù)據(jù)結(jié)構(gòu),具有特定數(shù)量和元素序列。比如設(shè)計(jì)一個(gè)三元組數(shù)據(jù)結(jié)構(gòu)用于存儲(chǔ)學(xué)生信息,一共包含三個(gè)元素,第一個(gè)是名字,第二個(gè)是年齡,第三個(gè)是身高。
元組的具體使用如下:
1.??? 如何創(chuàng)建元組
默認(rèn)情況.Net Framework元組僅支持1到7個(gè)元組元素,如果有8個(gè)元素或者更多,需要使用Tuple的嵌套和Rest屬性去實(shí)現(xiàn)。另外Tuple類提供創(chuàng)造元組對(duì)象的靜態(tài)方法。
利用構(gòu)造函數(shù)創(chuàng)建元組:
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靜態(tài)方法構(gòu)建元組,最多支持八個(gè)元素:
Note:這里構(gòu)建出來(lái)的Tuple類型其實(shí)是Tuple<int, int, int, int, int, int, int, Tuple<int>>,因此testTuple8.Rest取到的數(shù)據(jù)類型是Tuple<int>,因此要想獲取準(zhǔn)確值需要取Item1屬性。
2.??? 表示一組數(shù)據(jù)
如下創(chuàng)建一個(gè)元組表示一個(gè)學(xué)生的三個(gè)信息:名字、年齡和身高,而不用單獨(dú)額外創(chuàng)建一個(gè)類。
var studentInfo = Tuple.Create<string, int, uint>("Bob", 28, 175); Console.WriteLine($"Student Information: Name [{studentInfo.Item1}], Age [{studentInfo.Item2}], Height [{studentInfo.Item3}]");3.??? 從方法返回多個(gè)值
當(dāng)一個(gè)函數(shù)需要返回多個(gè)值的時(shí)候,一般情況下可以使用out參數(shù),這里可以用元組代替out實(shí)現(xiàn)返回多個(gè)值。
static Tuple<string, int, uint> GetStudentInfo(string name) { ??return new Tuple<string, int, uint>("Bob", 28, 175); }
static void RunTest() {
? ?var studentInfo = GetStudentInfo("Bob");Console.WriteLine($"Student Information: Name [{studentInfo.Item1}], Age [{studentInfo.Item2}], Height [{studentInfo.Item3}]"); }
4.??? 用于單參數(shù)方法的多值傳遞
當(dāng)函數(shù)參數(shù)僅是一個(gè)Object類型時(shí),可以使用元組實(shí)現(xiàn)傳遞多個(gè)參數(shù)值。
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}]"); }
static void RunTest() { ?
?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);} }
盡管元組有上述方便使用的方法,但是它也有明顯的不足:
訪問(wèn)元素的時(shí)候只能通過(guò)ItemX去訪問(wèn),使用前需要明確元素順序,屬性名字沒有實(shí)際意義,不方便記憶;
最多有八個(gè)元素,要想更多只能通過(guò)最后一個(gè)元素進(jìn)行嵌套擴(kuò)展;
Tuple是一個(gè)引用類型,不像其它的簡(jiǎn)單類型一樣是值類型,它在堆上分配空間,在CPU密集操作時(shí)可能有太多的創(chuàng)建和分配工作。
因此在C# 7.0中引入了一個(gè)新的ValueTuple類型,詳見下面章節(jié)。
ValueTuple詳解
ValueTuple是C# 7.0的新特性之一,.Net Framework 4.7以上版本可用。
值元組也是一種數(shù)據(jù)結(jié)構(gòu),用于表示特定數(shù)量和元素序列,但是是和元組類不一樣的,主要區(qū)別如下:
值元組是結(jié)構(gòu),是值類型,不是類,而元組(Tuple)是類,引用類型;
值元組元素是可變的,不是只讀的,也就是說(shuō)可以改變值元組中的元素值;
值元組的數(shù)據(jù)成員是字段不是屬性。
值元組的具體使用如下:
1.??? 如何創(chuàng)建值元組
和元組類一樣,.Net Framework值元組也只支持1到7個(gè)元組元素,如果有8個(gè)元素或者更多,需要使用值元組的嵌套和Rest屬性去實(shí)現(xiàn)。另外ValueTuple類可以提供創(chuàng)造值元組對(duì)象的靜態(tài)方法。
利用構(gòu)造函數(shù)創(chuàng)建元組:
利用Tuple靜態(tài)方法構(gòu)建元組,最多支持八個(gè)元素:
注意這里構(gòu)建出來(lái)的Tuple類型其實(shí)是Tuple<int, int, int, int, int, int, int, Tuple<int>>,因此testTuple8.Rest取到的數(shù)據(jù)類型是Tuple<int>,因此要想獲取準(zhǔn)確值需要取Item1屬性。
優(yōu)化區(qū)別:當(dāng)構(gòu)造出超過(guò)7個(gè)元素以上的值元組后,可以使用接下來(lái)的ItemX進(jìn)行訪問(wèn)嵌套元組中的值,對(duì)于上面的例子,要訪問(wèn)第十個(gè)元素,既可以通過(guò)testTuple10.Rest.Item3訪問(wèn),也可以通過(guò)testTuple10.Item10來(lái)訪問(wèn)。
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.??? 表示一組數(shù)據(jù)
如下創(chuàng)建一個(gè)值元組表示一個(gè)學(xué)生的三個(gè)信息:名字、年齡和身高,而不用單獨(dú)額外創(chuàng)建一個(gè)類。
var studentInfo = ValueTuple.Create<string, int, uint>("Bob", 28, 175); Console.WriteLine($"Student Information: Name [{studentInfo.Item1}], Age [{studentInfo.Item2}], Height [{studentInfo.Item3}]");3.??? 從方法返回多個(gè)值
值元組也可以在函數(shù)定義中代替out參數(shù)返回多個(gè)值。
static ValueTuple<string, int, uint> GetStudentInfo(string name) { ? ?return new ValueTuple <string, int, uint>("Bob", 28, 175); }static void RunTest() { ? ?var studentInfo = GetStudentInfo("Bob");Console.WriteLine($"Student Information: Name [{studentInfo.Item1}], Age [{studentInfo.Item2}], Height [{studentInfo.Item3}]"); }
優(yōu)化區(qū)別:返回值可以不明顯指定ValueTuple,使用新語(yǔ)法(,,)代替,如(string, int, uint):
static (string, int, uint) GetStudentInfo1(string name) { ??return ("Bob", 28, 175); }
static void RunTest1() { ? ?var studentInfo = GetStudentInfo1("Bob");Console.WriteLine($"Student Information: Name [{studentInfo.Item1}], Age [{studentInfo.Item2}], Height [{studentInfo.Item3}]"); }
調(diào)試查看studentInfo的類型就是ValueType三元組。
優(yōu)化區(qū)別:返回值可以指定元素名字,方便理解記憶賦值和訪問(wèn):
static (string name, int age, uint height) GetStudentInfo1(string name) { ? ?return ("Bob", 28, 175); }static void RunTest1() { ?
??var studentInfo = GetStudentInfo1("Bob");Console.WriteLine($"Student Information: Name [{studentInfo.name}], Age [{studentInfo.age}], Height [{studentInfo.height}]"); }
方便記憶賦值:
方便訪問(wèn):
4.??? 用于單參數(shù)方法的多值傳遞
當(dāng)函數(shù)參數(shù)僅是一個(gè)Object類型時(shí),可以使用值元組實(shí)現(xiàn)傳遞多個(gè)值。
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 RunTest() { ? ?
? ?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.??? 解構(gòu)ValueTuple
可以通過(guò)var (x, y)或者(var x, var y)來(lái)解析值元組元素構(gòu)造局部變量,同時(shí)可以使用符號(hào)”_”來(lái)忽略不需要的元素。
static (string name, int age, uint height) GetStudentInfo1(string name) { ? ?return ("Bob", 28, 175); }static void RunTest1() { ? ?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#變得更簡(jiǎn)單易用。較Tuple相比主要好處如下:
ValueTuple支持函數(shù)返回值新語(yǔ)法”(,,)”,使代碼更簡(jiǎn)單;
能夠給元素命名,方便使用和記憶,這里需要注意雖然命名了,但是實(shí)際上value tuple沒有定義這樣名字的屬性或者字段,真正的名字仍然是ItemX,所有的元素名字都只是設(shè)計(jì)和編譯時(shí)用的,不是運(yùn)行時(shí)用的(因此注意對(duì)該類型的序列化和反序列化操作);
可以使用解構(gòu)方法更方便地使用部分或全部元組的元素;
值元組是值類型,使用起來(lái)比引用類型的元組效率高,并且值元組是有比較方法的,可以用于比較是否相等,詳見:https://msdn.microsoft.com/en-us/library/system.valuetuple。
原文地址:http://www.cnblogs.com/lavender000/p/6916157.html
.NET社區(qū)新聞,深度好文,微信中搜索dotNET跨平臺(tái)或掃描二維碼關(guān)注
總結(jié)
以上是生活随笔為你收集整理的详解C# Tuple VS ValueTuple(元组类 VS 值元组)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 使用sqlserver搭建高可用双机热备
- 下一篇: 微软亚太区资料科学总监:R 语言是 VS