C# 元組是 C#.net 4.0 版本中引入的資料結構。元組資料結構旨在保存不同資料類型的元素。元組有助於從單一參數中的類別方法傳回多個值,這比輸出參數、類別或結構類型或動態傳回類型具有許多優點。由於參數被傳遞到單一資料集中,因此可以輕鬆存取該資料集並對其執行不同的操作。
元組可以用兩種不同的方式建立
用於建立元組的建構子存在於 Tuple
Tuple <T1> (T1)
範例:
Tuple<int> Tuple_example = new Tuple<int>(27); Console.WriteLine(Tuple_example); Console.ReadLine();
輸出:
Tuple <T1, T2> (T1, T2)
範例:
Tuple<int, string, bool> tuple = new Tuple<int, string, bool>(1, "cat", true); Console.WriteLine(tuple.Item1); Console.WriteLine(tuple.Item2.ToString()); Console.ReadLine();
輸出:
C#提供了靜態Create方法來建立元組,如下
Create (T1);
範例:
var Tuple_example = Tuple.Create(27); Console.WriteLine(Tuple_example); Console.ReadLine();
輸出:
Create (T1, T2);
範例:
var Tuple_example = Tuple.Create(1, "cat", true); Console.WriteLine(Tuple_example.Item1); Console.WriteLine(Tuple_example.Item2.ToString()); Console.ReadLine();
輸出:
使用建構函式時,我們需要在建立元組時指定每個元素的資料型態。 Create 方法幫助我們消除瞭如上所示的繁瑣編碼。
泛型元組是一種引用類型,這意味著值儲存在堆上,這使得它的使用在記憶體和效能方面成本高昂。 C#7.0 在通用元組的基礎上引入了新的改進版本的元組,並將其命名為 ValueTuple。 ValueTuple儲存在堆上,很容易檢索。此值元組隨 .NET Framework 4.7 或 .NET 函式庫 2.0 一起提供。若要單獨安裝元組功能,您需要安裝名為 System.Value.Tuple 的 NuGet 套件。
關於 ValueTuple 的要點
範例:
var Tuple_example = (1, "cat", true); Console.WriteLine(Tuple_example.Item1); Console.WriteLine(Tuple_example.Item2.ToString()); Console.ReadLine();
輸出:
這相當於:
var Tuple_example = Tuple.Create(1, "cat", true); Console.WriteLine(Tuple_example.Item1); Console.WriteLine(Tuple_example.Item2.ToString()); Console.ReadLine();
範例:
(int, string, bool) Tuple_example = (1, "cat", true); Console.WriteLine(Tuple_example.Item1); Console.WriteLine(Tuple_example.Item2.ToString()); Console.ReadLine();
輸出:
範例:
details.Item1; – returns 28 details.Item2; -- returns ”CBC”
範例:
var detail = (28); --this is not a tuple var details = (28, “CBC”); -- this is a tuple
在第一條語句中,編譯器不會將「detail」視為元組,而是將其視為普通的「var」類型。
(int ID, String Firstname, string SecondName) details = (28, “CBC”, “C# Tuples”);
var nestedtuple_example = new Tuple <int, string, string, int, int, int, string, Tuple<double, int, string>> (5, “This”, “is”, 7,8,9, “number”, Tuple.Create (17.33, 29,”April”));
{ Var multiplication = tupleexample.Item1 * tupleexample.Item2; Console.WriteLine (“Multiplication is”, {0}, multiplication); }
TupleExampleMethod 方法看起來像
TupleExampleMethod(new Tuple<int, int> (34,56));
public static Tuple <int, string> GetPerson() { return Tuple.Create (1, “abc”); }
Let’s create a program in Visual to understand how tuple works.
The values from both textboxes are taken into a tuple and the tuple is passed on to a method.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnMultiply_Click(object sender, EventArgs e) { int value1 = Convert.ToInt32(txtVal1.Text); int value2 = Convert.ToInt32(TxtVal2.Text); CallMethod(new Tuple<int, int>(value1, value2)); } private void CallMethod(Tuple<int, int> tuple) { txtResult.Text = Convert.ToString(tuple.Item1 * tuple.Item2); Console.ReadLine(); } } }
The result is displayed in the third text box named as txtResult. End result looks like.
The tuple data structure is a reference type, which means the values are stored on the heap instead of stack. This makes usage of tuples and accessing them in the program an intensive CPU task. The only 8 elements in tuples property is one of the major drawbacks of tuples as nested tuples are more prone to induce ambiguity. Also accessing elements in tuple with Item
以上是C# 元組的詳細內容。更多資訊請關注PHP中文網其他相關文章!