欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C# 元組和值元組的具體使用

 更新時(shí)間:2020年10月29日 11:58:15   作者:北田  
這篇文章主要介紹了C# 元組和值元組的具體使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

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

如果您對(duì)Tuple足夠了解,可以直接跳過章節(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 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)建元組,最多支持八個(gè)元素:

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類型其實(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);
  }
}

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

  • 訪問元素的時(shí)候只能通過ItemX去訪問,使用前需要明確元素順序,屬性名字沒有實(shí)際意義,不方便記憶;
  • 最多有八個(gè)元素,要想更多只能通過最后一個(gè)元素進(jìn)行嵌套擴(kuò)展;
  • Tuple是一個(gè)引用類型,不像其它的簡單類型一樣是值類型,它在堆上分配空間,在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)是類,引用類型;
  • 值元組元素是可變的,不是只讀的,也就是說可以改變值元組中的元素值;
  • 值元組的數(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)建元組:

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)建元組,最多支持八個(gè)元素:

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類型其實(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)造出超過7個(gè)元素以上的值元組后,可以使用接下來的ItemX進(jìn)行訪問嵌套元組中的值,對(duì)于上面的例子,要訪問第十個(gè)元素,既可以通過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)建一個(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,使用新語法(,,)代替,如(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ù)方法的多值傳遞

當(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

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

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ù)返回值新語法”(,,)”,使代碼更簡單;
  • 能夠給元素命名,方便使用和記憶,這里需要注意雖然命名了,但是實(shí)際上value tuple沒有定義這樣名字的屬性或者字段,真正的名字仍然是ItemX,所有的元素名字都只是設(shè)計(jì)和編譯時(shí)用的,不是運(yùn)行時(shí)用的(因此注意對(duì)該類型的序列化和反序列化操作);
  • 可以使用解構(gòu)方法更方便地使用部分或全部元組的元素;
  • 值元組是值類型,使用起來比引用類型的元組效率高,并且值元組是有比較方法的,可以用于比較是否相等,詳見:https://msdn.microsoft.com/en-us/library/system.valuetuple。

到此這篇關(guān)于C# 元組和值元組的具體使用的文章就介紹到這了,更多相關(guān)C# 元組和值元組內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C#實(shí)現(xiàn)用于生成條形碼的類

    C#實(shí)現(xiàn)用于生成條形碼的類

    這篇文章主要介紹了C#實(shí)現(xiàn)用于生成條形碼的類,涉及C#生成條形碼的原理與實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-03-03
  • C#圖片添加水印的實(shí)現(xiàn)代碼

    C#圖片添加水印的實(shí)現(xiàn)代碼

    這篇文章主要為大家詳細(xì)介紹了C#給圖片添加水印的實(shí)現(xiàn)代碼,不僅可以為圖片加文字水印,還可以判斷是否是圖片文件,感興趣的小伙伴們可以參考一下
    2016-02-02
  • C#文件斷點(diǎn)續(xù)傳實(shí)現(xiàn)方法

    C#文件斷點(diǎn)續(xù)傳實(shí)現(xiàn)方法

    這篇文章主要介紹了C#文件斷點(diǎn)續(xù)傳實(shí)現(xiàn)方法,涉及C#文件傳輸?shù)募记?具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-08-08
  • 采用C#實(shí)現(xiàn)軟件自動(dòng)更新的方法

    采用C#實(shí)現(xiàn)軟件自動(dòng)更新的方法

    這篇文章主要介紹了采用C#實(shí)現(xiàn)軟件自動(dòng)更新的方法,非常實(shí)用的功能,需要的朋友可以參考下
    2014-08-08
  • C#中實(shí)現(xiàn)任意List的全組合算法代碼

    C#中實(shí)現(xiàn)任意List的全組合算法代碼

    這篇文章主要是介紹了.net C# 實(shí)現(xiàn)任意List的全組合算法實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2013-05-05
  • C#線程池用法詳細(xì)介紹

    C#線程池用法詳細(xì)介紹

    在C#編程語言中,使用線程池可以并行地處理工作,當(dāng)強(qiáng)制線程和更新進(jìn)度條時(shí),會(huì)使用內(nèi)建架構(gòu)的ThreadPool類,為批處理使用多核結(jié)構(gòu),這里我們來看在C#編程語言中一些關(guān)于來自System.Threading的ThreadPool的用法的例子
    2013-11-11
  • C#從實(shí)體對(duì)象集合中導(dǎo)出Excel的代碼

    C#從實(shí)體對(duì)象集合中導(dǎo)出Excel的代碼

    數(shù)據(jù)的導(dǎo)出是項(xiàng)目中經(jīng)常要實(shí)現(xiàn)的功能,就拿最常見的要導(dǎo)出成Excel來說,網(wǎng)上看來看去,都是介紹從Datatable中導(dǎo)出
    2008-08-08
  • Unity實(shí)現(xiàn)枚舉類型中文顯示

    Unity實(shí)現(xiàn)枚舉類型中文顯示

    這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)枚舉類型中文顯示,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-02-02
  • C#多線程系列之工作流實(shí)現(xiàn)

    C#多線程系列之工作流實(shí)現(xiàn)

    本文詳細(xì)講解了C#實(shí)現(xiàn)多線程工作流的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-02-02
  • C#計(jì)算矩陣的逆矩陣方法實(shí)例分析

    C#計(jì)算矩陣的逆矩陣方法實(shí)例分析

    這篇文章主要介紹了C#計(jì)算矩陣的逆矩陣方法,較為詳細(xì)的分析了逆矩陣的計(jì)算原理與相關(guān)的C#實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-08-08

最新評(píng)論