C# 元组

WBOY
发布: 2024-09-03 15:30:55
原创
606 人浏览过

C# 元组是 C#.net 4.0 版本中引入的一种数据结构。元组数据结构旨在保存不同数据类型的元素。元组有助于从单个参数中的类方法返回多个值,这比输出参数、类或结构类型或动态返回类型具有许多优点。由于参数被传递到单个数据集中,因此可以轻松访问该数据集并对其执行不同的操作。

如何创建 C# 元组?

元组可以通过两种不同的方式创建

1.使用构造函数

用于创建元组的构造函数存在于 Tuple 中;班级。首字母缩略词“T”表示创建元组时指定的多种数据类型。元组中存储的元素编号为 0 到 7,也就是说任何普通元组仅包含 8 个元素,如果尝试输入超过 8 个元素,编译器会抛出错误。

单元素元组
Tuple <T1> (T1)
登录后复制

示例:

Tuple<int> Tuple_example = new Tuple<int>(27);
Console.WriteLine(Tuple_example);
Console.ReadLine();
登录后复制

输出:

C# 元组

多元素元组
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# 元组

2.创建方法

C#提供了静态Create方法来创建元组,如下

单元素元组
Create (T1);
登录后复制

示例:

var Tuple_example = Tuple.Create(27);
Console.WriteLine(Tuple_example);
Console.ReadLine();
登录后复制

输出:

C# 元组

多元素元组
Create (T1, T2);
登录后复制

示例:

var Tuple_example = Tuple.Create(1, "cat", true);
Console.WriteLine(Tuple_example.Item1);
Console.WriteLine(Tuple_example.Item2.ToString());
Console.ReadLine();
登录后复制
登录后复制

输出:

C# 元组

使用构造函数时,我们需要在创建元组时指定每个元素的数据类型。 Create 方法帮助我们消除了如上所示的繁琐编码。

值元组

泛型元组是一种引用类型,这意味着值存储在堆上,这使得它的使用在内存和性能方面成本高昂。 C#7.0 在通用元组的基础上引入了新的改进版本的元组,并将其命名为 ValueTuple。 ValueTuple存储在堆上,很容易检索。该值元组随 .NET Framework 4.7 或 .NET 库 2.0 一起提供。要单独安装元组功能,您需要安装名为 System.Value.Tuple 的 NuGet 包。

关于 ValueTuple 的要点

  • 创建 ValueTuple 很容易

示例:

var Tuple_example = (1, "cat", true);
Console.WriteLine(Tuple_example.Item1);
Console.WriteLine(Tuple_example.Item2.ToString());
Console.ReadLine();
登录后复制

输出:

C# 元组

这相当于:

var Tuple_example = Tuple.Create(1, "cat", true);
Console.WriteLine(Tuple_example.Item1);
Console.WriteLine(Tuple_example.Item2.ToString());
Console.ReadLine();
登录后复制
登录后复制
  • ValueTuple 也可以在不使用“var”关键字的情况下声明。在这种情况下,我们需要提供每个成员的数据类型

示例:

(int, string, bool) Tuple_example = (1, "cat", true);
Console.WriteLine(Tuple_example.Item1);
Console.WriteLine(Tuple_example.Item2.ToString());
Console.ReadLine();
登录后复制

输出:

C# 元组

  • 可以使用
  • 从 ValueTuple 返回值

示例:

details.Item1;   – returns 28
details.Item2; -- returns ”CBC”
登录后复制
  • ValueTuple 与普通元组不同,不能只包含一个元素。

示例:

var detail = (28);  --this is not a tuple
var details = (28, “CBC”); -- this is a tuple
登录后复制

在第一条语句中,编译器不会将“detail”视为元组,而是将其视为普通的“var”类型。

  • ValueTuple 可以容纳八个以上的值,而无需在第七个位置嵌套另一个元组。
  • ValueTuple 中的属性可以具有与 Item1、Item2 等不同的名称。
(int ID, String Firstname, string SecondName) details = (28, “CBC”, “C# Tuples”);
登录后复制
  • ValueTuple中的元素也可以根据编程的需要进行分离或丢弃。在上面的示例中,可以丢弃元素“FirstName”,并可以将包含第一个元素和第三个元素的元组作为方法的返回类型传递。

元组如何工作?

  1. C# 框架只允许元组中有 8 个元素,这意味着我们可以使用 0 到 7 之间的值,如果您想创建一个包含更多元素的元组,则将第七个元素 TRest 指定为嵌套元组
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”));
登录后复制
  1. 元组的一个重要用途是将其作为单个实体传递给方法,而不使用传统的“out”和“ref”关键字。 “Out”和“ref”参数的使用可能会很困难且令人困惑,而且“out”和“ref”参数不适用于“asnyc”方法。例如public void TupleExampleMethod (Tuple tupleexample)
{
Var multiplication = tupleexample.Item1 * tupleexample.Item2;
Console.WriteLine (“Multiplication is”, {0}, multiplication);
}
登录后复制

TupleExampleMethod 方法看起来像

TupleExampleMethod(new Tuple<int, int> (34,56));
登录后复制
  1. The dynamic keyword can also be used to return values from any method, but it is seldom used due to performance issues. The returning of the tuple from a method.
public static Tuple <int, string> GetPerson()
{
return Tuple.Create (1, “abc”);
}
登录后复制

Let’s create a program in Visual to understand how tuple works.

  • Launch Visual Studio and create a windows project.

C# 元组

  • We are creating a simple multiplication program that shows passing tuples by a method. A sample window created as below.

C# 元组

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.

C# 元组

Conclusion

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 is also ambiguous as one must remember what position the element is on in order to access it. C#7 has introduced ValueTuple which is value type representation of tuple. It works only on .NET Framework 4.7 and hence needs to install separately from the Nuget package System.ValueTuple package.

以上是C# 元组的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!