国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

目錄
如何創(chuàng)建 C# 元組?
1.使用構造函數(shù)
單元素元組
多元素元組
2.創(chuàng)建方法
值元組
元組如何工作?
Conclusion

C# 元組

Sep 03, 2024 pm 03:30 PM
c# c# tutorial

C# 元組是 C#.net 4.0 版本中引入的一種數(shù)據(jù)結構。元組數(shù)據(jù)結構旨在保存不同數(shù)據(jù)類型的元素。元組有助于從單個參數(shù)中的類方法返回多個值,這比輸出參數(shù)、類或結構類型或動態(tài)返回類型具有許多優(yōu)點。由于參數(shù)被傳遞到單個數(shù)據(jù)集中,因此可以輕松訪問該數(shù)據(jù)集并對其執(zhí)行不同的操作。

如何創(chuàng)建 C# 元組?

元組可以通過兩種不同的方式創(chuàng)建

1.使用構造函數(shù)

用于創(chuàng)建元組的構造函數(shù)存在于 Tuple 中;班級。首字母縮略詞“T”表示創(chuàng)建元組時指定的多種數(shù)據(jù)類型。元組中存儲的元素編號為 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.創(chuàng)建方法

C#提供了靜態(tài)Create方法來創(chuàng)建元組,如下

單元素元組
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# 元組

使用構造函數(shù)時,我們需要在創(chuàng)建元組時指定每個元素的數(shù)據(jù)類型。 Create 方法幫助我們消除了如上所示的繁瑣編碼。

值元組

泛型元組是一種引用類型,這意味著值存儲在堆上,這使得它的使用在內存和性能方面成本高昂。 C#7.0 在通用元組的基礎上引入了新的改進版本的元組,并將其命名為 ValueTuple。 ValueTuple存儲在堆上,很容易檢索。該值元組隨 .NET Framework 4.7 或 .NET 庫 2.0 一起提供。要單獨安裝元組功能,您需要安裝名為 System.Value.Tuple 的 NuGet 包。

關于 ValueTuple 的要點

  • 創(chuàng)建 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”關鍵字的情況下聲明。在這種情況下,我們需要提供每個成員的數(shù)據(jù)類型

示例:

(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中的元素也可以根據(jù)編程的需要進行分離或丟棄。在上面的示例中,可以丟棄元素“FirstName”,并可以將包含第一個元素和第三個元素的元組作為方法的返回類型傳遞。

元組如何工作?

  1. C# 框架只允許元組中有 8 個元素,這意味著我們可以使用 0 到 7 之間的值,如果您想創(chuàng)建一個包含更多元素的元組,則將第七個元素 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. 元組的一個重要用途是將其作為單個實體傳遞給方法,而不使用傳統(tǒng)的“out”和“ref”關鍵字。 “Out”和“ref”參數(shù)的使用可能會很困難且令人困惑,而且“out”和“ref”參數(shù)不適用于“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中文網其他相關文章!

本站聲明
本文內容由網友自發(fā)貢獻,版權歸原作者所有,本站不承擔相應法律責任。如您發(fā)現(xiàn)有涉嫌抄襲侵權的內容,請聯(lián)系admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣服圖片

Undresser.AI Undress

Undresser.AI Undress

人工智能驅動的應用程序,用于創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用于從照片中去除衣服的在線人工智能工具。

Clothoff.io

Clothoff.io

AI脫衣機

Video Face Swap

Video Face Swap

使用我們完全免費的人工智能換臉工具輕松在任何視頻中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的代碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

功能強大的PHP集成開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級代碼編輯軟件(SublimeText3)

C# 中的隨機數(shù)生成器 C# 中的隨機數(shù)生成器 Sep 03, 2024 pm 03:34 PM

C# 隨機數(shù)生成器指南。在這里,我們討論隨機數(shù)生成器的工作原理、偽隨機數(shù)和安全數(shù)的概念。

C# 中的階乘 C# 中的階乘 Sep 03, 2024 pm 03:34 PM

C# 階乘指南。這里我們討論 C# 中階乘的介紹以及不同的示例和代碼實現(xiàn)。

c#多線程和異步的區(qū)別 c#多線程和異步的區(qū)別 Apr 03, 2025 pm 02:57 PM

多線程和異步的區(qū)別在于,多線程同時執(zhí)行多個線程,而異步在不阻塞當前線程的情況下執(zhí)行操作。多線程用于計算密集型任務,而異步用于用戶交互操作。多線程的優(yōu)勢是提高計算性能,異步的優(yōu)勢是不阻塞 UI 線程。選擇多線程還是異步取決于任務性質:計算密集型任務使用多線程,與外部資源交互且需要保持 UI 響應的任務使用異步。

C# 中的質數(shù) C# 中的質數(shù) Sep 03, 2024 pm 03:35 PM

C# 素數(shù)指南。這里我們討論c#中素數(shù)的介紹和示例以及代碼實現(xiàn)。

C#與C:歷史,進化和未來前景 C#與C:歷史,進化和未來前景 Apr 19, 2025 am 12:07 AM

C#和C 的歷史與演變各有特色,未來前景也不同。1.C 由BjarneStroustrup在1983年發(fā)明,旨在將面向對象編程引入C語言,其演變歷程包括多次標準化,如C 11引入auto關鍵字和lambda表達式,C 20引入概念和協(xié)程,未來將專注于性能和系統(tǒng)級編程。2.C#由微軟在2000年發(fā)布,結合C 和Java的優(yōu)點,其演變注重簡潔性和生產力,如C#2.0引入泛型,C#5.0引入異步編程,未來將專注于開發(fā)者的生產力和云計算。

C# 中的模式 C# 中的模式 Sep 03, 2024 pm 03:33 PM

C# 模式指南。在這里,我們討論 C# 中模式的介紹和前 3 種類型,以及其示例和代碼實現(xiàn)。

xml怎么改格式 xml怎么改格式 Apr 03, 2025 am 08:42 AM

可以采用多種方法修改 XML 格式:使用文本編輯器(如 Notepad )進行手工編輯;使用在線或桌面 XML 格式化工具(如 XMLbeautifier)進行自動格式化;使用 XML 轉換工具(如 XSLT)定義轉換規(guī)則;或者使用編程語言(如 Python)進行解析和操作。修改時需謹慎,并備份原始文件。

C# 中的回文 C# 中的回文 Sep 03, 2024 pm 03:34 PM

C# 回文指南。在這里,我們討論 C# 中回文背后的介紹和邏輯,以及各種方法及其代碼。

See all articles