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

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

C# 元組

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

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

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

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

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

用于創(chuàng)建元組的構(gòu)造函數(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# 元組

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

值元組

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

關(guān)于 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”關(guān)鍵字的情況下聲明。在這種情況下,我們需要提供每個成員的數(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”關(guān)鍵字。 “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# 元組的詳細內(nèi)容。更多信息請關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

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

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣服圖片

Undresser.AI Undress

Undresser.AI Undress

人工智能驅(qū)動的應(yīng)用程序,用于創(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

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

SublimeText3 Mac版

SublimeText3 Mac版

神級代碼編輯軟件(SublimeText3)

熱門話題

Laravel 教程
1601
29
PHP教程
1502
276
c#多線程和異步的區(qū)別 c#多線程和異步的區(qū)別 Apr 03, 2025 pm 02:57 PM

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

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

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

xml怎么轉(zhuǎn)換成json xml怎么轉(zhuǎn)換成json Apr 03, 2025 am 09:09 AM

將 XML 轉(zhuǎn)換為 JSON 的方法包括:使用編程語言(如 Python、Java、C#)編寫腳本或程序進行轉(zhuǎn)換;使用在線工具(如 XML 轉(zhuǎn)換為 JSON、Gojko's XML 轉(zhuǎn)換器、XML 在線工具)粘貼或上傳 XML 數(shù)據(jù)并選擇 JSON 格式輸出;使用 XML 到 JSON 轉(zhuǎn)換器(如 Oxygen XML Editor、Stylus Studio、Altova XMLSpy)執(zhí)行轉(zhuǎn)換任務(wù);使用 XSLT 樣式表將 XML 轉(zhuǎn)換為 JSON;使用數(shù)據(jù)集成工具(如 Informatic

c#多線程編程是什么  c#多線程編程用處 c#多線程編程是什么 c#多線程編程用處 Apr 03, 2025 pm 02:45 PM

C# 多線程編程是一種讓程序同時執(zhí)行多項任務(wù)的技術(shù),它可以通過提升性能、提高響應(yīng)能力和實現(xiàn)并行處理來提高程序效率。雖然 Thread 類提供了直接創(chuàng)建線程的方法,但 Task 和 async/await 等高級工具可以提供更安全的異步操作和更簡潔的代碼結(jié)構(gòu)。多線程編程中常見的難題包括死鎖、競態(tài)條件和資源泄漏,需要仔細設(shè)計線程模型和使用適當?shù)耐綑C制來避免這些問題。

C#.NET:使用.NET生態(tài)系統(tǒng)構(gòu)建應(yīng)用程序 C#.NET:使用.NET生態(tài)系統(tǒng)構(gòu)建應(yīng)用程序 Apr 27, 2025 am 12:12 AM

如何利用.NET構(gòu)建應(yīng)用?使用.NET構(gòu)建應(yīng)用可以通過以下步驟實現(xiàn):1)了解.NET基礎(chǔ)知識,包括C#語言和跨平臺開發(fā)支持;2)學習核心概念,如.NET生態(tài)系統(tǒng)的組件和工作原理;3)掌握基本和高級用法,從簡單控制臺應(yīng)用到復(fù)雜的WebAPI和數(shù)據(jù)庫操作;4)熟悉常見錯誤與調(diào)試技巧,如配置和數(shù)據(jù)庫連接問題;5)應(yīng)用性能優(yōu)化與最佳實踐,如異步編程和緩存。

從網(wǎng)絡(luò)到桌面:C#.NET的多功能性 從網(wǎng)絡(luò)到桌面:C#.NET的多功能性 Apr 15, 2025 am 12:07 AM

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

c#多線程的好處有哪些 c#多線程的好處有哪些 Apr 03, 2025 pm 02:51 PM

多線程的好處在于能提升性能和資源利用率,尤其適用于處理大量數(shù)據(jù)或執(zhí)行耗時操作。它允許同時執(zhí)行多個任務(wù),提高效率。然而,線程過多會導(dǎo)致性能下降,因此需要根據(jù) CPU 核心數(shù)和任務(wù)特性謹慎選擇線程數(shù)。另外,多線程編程涉及死鎖和競態(tài)條件等挑戰(zhàn),需要使用同步機制解決,需要具備扎實的并發(fā)編程知識,權(quán)衡利弊并謹慎使用。

.NET框架與C#:解碼術(shù)語 .NET框架與C#:解碼術(shù)語 Apr 21, 2025 am 12:05 AM

.NETFramework是一個軟件框架,C#是一種編程語言。1..NETFramework提供庫和服務(wù),支持桌面、Web和移動應(yīng)用開發(fā)。2.C#設(shè)計用于.NETFramework,支持現(xiàn)代編程功能。3..NETFramework通過CLR管理代碼執(zhí)行,C#代碼編譯成IL后由CLR運行。4.使用.NETFramework可快速開發(fā)應(yīng)用,C#提供如LINQ的高級功能。5.常見錯誤包括類型轉(zhuǎn)換和異步編程死鎖,調(diào)試需用VisualStudio工具。

See all articles