日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

详解C# Tuple VS ValueTuple(元组类 VS 值元组)

發(fā)布時間:2023/12/4 C# 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 详解C# Tuple VS ValueTuple(元组类 VS 值元组) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

C# 7.0已經(jīng)出來一段時間了,大家都知道新特性里面有個對元組的優(yōu)化:ValueTuple。這里利用詳盡的例子詳解Tuple VS ValueTuple(元組類VS值元組),10分鐘讓你更了解ValueTuple的好處和用法。

如果您對Tuple足夠了解,可以直接跳過章節(jié)”回顧Tuple”,直達章節(jié)”ValueTuple詳解”,查看值元組的炫麗用法。

回顧Tuple

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

元組是一種數(shù)據(jù)結(jié)構(gòu),具有特定數(shù)量和元素序列。比如設(shè)計一個三元組數(shù)據(jù)結(jié)構(gòu)用于存儲學(xué)生信息,一共包含三個元素,第一個是名字,第二個是年齡,第三個是身高。

元組的具體使用如下:

1.??? 如何創(chuàng)建元組

默認情況.Net Framework元組僅支持1到7個元組元素,如果有8個元素或者更多,需要使用Tuple的嵌套和Rest屬性去實現(xiàn)。另外Tuple類提供創(chuàng)造元組對象的靜態(tài)方法。

  • 利用構(gòu)造函數(shù)創(chuàng)建元組:

var testTuple6 = new Tuple<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 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)建元組,最多支持八個元素:

var testTuple6 = Tuple.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 = 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:這里構(gòu)建出來的Tuple類型其實是Tuple<int, int, int, int, int, int, int, Tuple<int>>,因此testTuple8.Rest取到的數(shù)據(jù)類型是Tuple<int>,因此要想獲取準確值需要取Item1屬性。

2.??? 表示一組數(shù)據(jù)

如下創(chuàng)建一個元組表示一個學(xué)生的三個信息:名字、年齡和身高,而不用單獨額外創(chuàng)建一個類。

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

3.??? 從方法返回多個值

當一個函數(shù)需要返回多個值的時候,一般情況下可以使用out參數(shù),這里可以用元組代替out實現(xiàn)返回多個值。

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ù)方法的多值傳遞

當函數(shù)參數(shù)僅是一個Object類型時,可以使用元組實現(xiàn)傳遞多個參數(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);} }


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

  • 訪問元素的時候只能通過ItemX去訪問,使用前需要明確元素順序,屬性名字沒有實際意義,不方便記憶;

  • 最多有八個元素,要想更多只能通過最后一個元素進行嵌套擴展;

  • Tuple是一個引用類型,不像其它的簡單類型一樣是值類型,它在堆上分配空間,在CPU密集操作時可能有太多的創(chuàng)建和分配工作。

因此在C# 7.0中引入了一個新的ValueTuple類型,詳見下面章節(jié)。

ValueTuple詳解

ValueTuple是C# 7.0的新特性之一,.Net Framework 4.7以上版本可用。

值元組也是一種數(shù)據(jù)結(jié)構(gòu),用于表示特定數(shù)量和元素序列,但是是和元組類不一樣的,主要區(qū)別如下:

  • 值元組是結(jié)構(gòu),是值類型,不是類,而元組(Tuple)是類,引用類型;

  • 值元組元素是可變的,不是只讀的,也就是說可以改變值元組中的元素值;

  • 值元組的數(shù)據(jù)成員是字段不是屬性。

值元組的具體使用如下:

1.??? 如何創(chuàng)建值元組

和元組類一樣,.Net Framework值元組也只支持1到7個元組元素,如果有8個元素或者更多,需要使用值元組的嵌套和Rest屬性去實現(xiàn)。另外ValueTuple類可以提供創(chuàng)造值元組對象的靜態(tài)方法。

  • 利用構(gòu)造函數(shù)創(chuàng)建元組:

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靜態(tài)方法構(gòu)建元組,最多支持八個元素:

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}");

注意這里構(gòu)建出來的Tuple類型其實是Tuple<int, int, int, int, int, int, int, Tuple<int>>,因此testTuple8.Rest取到的數(shù)據(jù)類型是Tuple<int>,因此要想獲取準確值需要取Item1屬性。

優(yōu)化區(qū)別:當構(gòu)造出超過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.??? 表示一組數(shù)據(jù)

如下創(chuàng)建一個值元組表示一個學(xué)生的三個信息:名字、年齡和身高,而不用單獨額外創(chuàng)建一個類。

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

3.??? 從方法返回多個值

值元組也可以在函數(shù)定義中代替out參數(shù)返回多個值。

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,使用新語法(,,)代替,如(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ū)別:返回值可以指定元素名字,方便理解記憶賦值和訪問:

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}]"); }


方便記憶賦值:

方便訪問:

4.??? 用于單參數(shù)方法的多值傳遞

當函數(shù)參數(shù)僅是一個Object類型時,可以使用值元組實現(xiàn)傳遞多個值。

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

可以通過var (x, y)或者(var x, var y)來解析值元組元素構(gòu)造局部變量,同時可以使用符號”_”來忽略不需要的元素。

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#變得更簡單易用。較Tuple相比主要好處如下:

  • ValueTuple支持函數(shù)返回值新語法”(,,)”,使代碼更簡單;

  • 能夠給元素命名,方便使用和記憶,這里需要注意雖然命名了,但是實際上value tuple沒有定義這樣名字的屬性或者字段,真正的名字仍然是ItemX,所有的元素名字都只是設(shè)計和編譯時用的,不是運行時用的(因此注意對該類型的序列化和反序列化操作);

  • 可以使用解構(gòu)方法更方便地使用部分或全部元組的元素;

  • 值元組是值類型,使用起來比引用類型的元組效率高,并且值元組是有比較方法的,可以用于比較是否相等,詳見:https://msdn.microsoft.com/en-us/library/system.valuetuple。

原文地址:http://www.cnblogs.com/lavender000/p/6916157.html


.NET社區(qū)新聞,深度好文,微信中搜索dotNET跨平臺或掃描二維碼關(guān)注

總結(jié)

以上是生活随笔為你收集整理的详解C# Tuple VS ValueTuple(元组类 VS 值元组)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。