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

目錄
如何建立 C# 元組?
1.使用建構(gòu)子
單一元素元組
多元素元組
2.建立方法
值元組
元組如何運(yùn)作?
Conclusion

C# 元組

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

C# 元組是 C#.net 4.0 版本中引入的資料結(jié)構(gòu)。元組資料結(jié)構(gòu)旨在保存不同資料類型的元素。元組有助於從單一參數(shù)中的類別方法傳回多個(gè)值,這比輸出參數(shù)、類別或結(jié)構(gòu)類型或動(dòng)態(tài)傳回類型具有許多優(yōu)點(diǎn)。由於參數(shù)被傳遞到??單一資料集中,因此可以輕鬆存取該資料集並對(duì)其執(zhí)行不同的操作。

如何建立 C# 元組?

元組可以用兩種不同的方式建立

1.使用建構(gòu)子

用於建立元組的建構(gòu)子存在於 Tuple 中;班級(jí)。首字母縮寫“T”表示創(chuàng)建元組時(shí)指定的多種資料類型。元組中儲(chǔ)存的元素編號(hào)為 0 到 7,也就是說任何普通元組只包含 8 個(gè)元素,如果嘗試輸入超過 8 個(gè)元素,編譯器會(huì)拋出錯(cuò)誤。

單一元素元組
Tuple <T1> (T1)

範(fàn)例:

Tuple<int> Tuple_example = new Tuple<int>(27);
Console.WriteLine(Tuple_example);
Console.ReadLine();

輸出:

C# 元組

多元素元組
Tuple <T1, T2> (T1, T2)

範(fàn)例:

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#提供了靜態(tài)Create方法來建立元組,如下

單一元素元組
Create (T1);

範(fàn)例:

var Tuple_example = Tuple.Create(27);
Console.WriteLine(Tuple_example);
Console.ReadLine();

輸出:

C# 元組

多元素元組
Create (T1, T2);

範(fàn)例:

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

輸出:

C# 元組

使用建構(gòu)函式時(shí),我們需要在建立元組時(shí)指定每個(gè)元素的資料型態(tài)。 Create 方法幫助我們消除瞭如上所示的繁瑣編碼。

值元組

泛型元組是一種引用類型,這意味著值儲(chǔ)存在堆上,這使得它的使用在記憶體和效能方面成本高昂。 C#7.0 在通用元組的基礎(chǔ)上引入了新的改進(jìn)版本的元組,並將其命名為 ValueTuple。 ValueTuple儲(chǔ)存在堆上,很容易檢索。此值元組隨 .NET Framework 4.7 或 .NET 函式庫 2.0 一起提供。若要單獨(dú)安裝元組功能,您需要安裝名為 System.Value.Tuple 的 NuGet 套件。

關(guān)於 ValueTuple 的要點(diǎn)

  • 建立 ValueTuple 很容易

範(fàn)例:

var Tuple_example = (1, "cat", true);
Console.WriteLine(Tuple_example.Item1);
Console.WriteLine(Tuple_example.Item2.ToString());
Console.ReadLine();

輸出:

C# 元組

這相當(dāng)於:

var Tuple_example = Tuple.Create(1, "cat", true);
Console.WriteLine(Tuple_example.Item1);
Console.WriteLine(Tuple_example.Item2.ToString());
Console.ReadLine();
  • ValueTuple 也可以在不使用「var」關(guān)鍵字的情況下聲明。在這種情況下,我們需要提供每個(gè)成員的資料類型

範(fàn)例:

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

輸出:

C# 元組

  • 可以使用
  • 從 ValueTuple 傳回值

範(fàn)例:

details.Item1;?? – returns 28
details.Item2; -- returns ”CBC”
  • ValueTuple 與普通元組不同,不能只包含一個(gè)元素。

範(fàn)例:

var detail = (28);? --this is not a tuple
var details = (28, “CBC”); -- this is a tuple

在第一條語句中,編譯器不會(huì)將「detail」視為元組,而是將其視為普通的「var」類型。

  • ValueTuple 可以容納八個(gè)以上的值,而無需在第七個(gè)位置嵌套另一個(gè)元組。
  • ValueTuple 中的屬性可以有與 Item1、Item2 等不同的名稱。
(int ID, String Firstname, string SecondName) details = (28, “CBC”, “C# Tuples”);
  • ValueTuple中的元素也可以依照程式設(shè)計(jì)的需要進(jìn)行分離或丟棄。在上面的範(fàn)例中,可以丟棄元素“FirstName”,並且可以將包含第一個(gè)元素和第三個(gè)元素的元組作為方法的傳回類型傳遞。

元組如何運(yùn)作?

  1. C# 框架只允許元組中有8 個(gè)元素,這表示我們可以使用0 到7 之間的值,如果您想要建立包含更多元素的元組,則將第七個(gè)元素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. 元組的一個(gè)重要用途是將其作為單一實(shí)體傳遞給方法,而不使用傳統(tǒng)的「out」和「ref」關(guān)鍵字。 「Out」和「ref」參數(shù)的使用可能會(huì)很困難且令人困惑,而且「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# 元組的詳細(xì)內(nèi)容。更多資訊請(qǐng)關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

本網(wǎng)站聲明
本文內(nèi)容由網(wǎng)友自願(yuàn)投稿,版權(quán)歸原作者所有。本站不承擔(dān)相應(yīng)的法律責(zé)任。如發(fā)現(xiàn)涉嫌抄襲或侵權(quán)的內(nèi)容,請(qǐng)聯(lián)絡(luò)admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費(fèi)脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅(qū)動(dòng)的應(yīng)用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費(fèi)的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費(fèi)的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強(qiáng)大的PHP整合開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

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

SublimeText3 Mac版

SublimeText3 Mac版

神級(jí)程式碼編輯軟體(SublimeText3)

C# 中的隨機(jī)數(shù)產(chǎn)生器 C# 中的隨機(jī)數(shù)產(chǎn)生器 Sep 03, 2024 pm 03:34 PM

C# 隨機(jī)數(shù)產(chǎn)生器指南。在這裡,我們討論隨機(jī)數(shù)產(chǎn)生器的工作原理、偽隨機(jī)數(shù)和安全數(shù)的概念。

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

C# 階乘指南。這裡我們討論 C# 中階乘的介紹以及不同的範(fàn)例和程式碼實(shí)作。

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

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

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

C# 質(zhì)數(shù)指南。這裡我們討論c#中素?cái)?shù)的介紹和範(fàn)例以及程式碼實(shí)作。

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

C#和C 的歷史與演變各有特色,未來前景也不同。 1.C 由BjarneStroustrup在1983年發(fā)明,旨在將面向?qū)ο缶幊桃隒語言,其演變歷程包括多次標(biāo)準(zhǔn)化,如C 11引入auto關(guān)鍵字和lambda表達(dá)式,C 20引入概念和協(xié)程,未來將專注於性能和系統(tǒng)級(jí)編程。 2.C#由微軟在2000年發(fā)布,結(jié)合C 和Java的優(yōu)點(diǎn),其演變注重簡潔性和生產(chǎn)力,如C#2.0引入泛型,C#5.0引入異步編程,未來將專注於開發(fā)者的生產(chǎn)力和雲(yún)計(jì)算。

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

C# 模式指南。在這裡,我們討論 C# 中模式的介紹和前 3 種類型,以及其範(fàn)例和程式碼實(shí)作。

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

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

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

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

See all articles