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中文网其他相关文章!