元組C# 類
元組類<T1、T2> 表示 2 元組,稱為對。元組是一個包含一系列元素的資料結構。
其用途有:
- 更容易訪問資料集。
- 更容易處理資料集。
- 用於表示單一資料集。
- 用於從方法返回多個值
- 用於將多個值傳遞給方法
它有兩個屬性:
項 1 - 獲取當前元組<T1、T2> 物件第一個分量的值。
項 2 - 獲取當前元組<T1、T2> 物件第二個分量的值。
示例
現在讓我們來看一個在 C# 中實現 2 元組的示例:
using System;
public class Demo {
public static void Main(string[] args) {
Tuple<string,string> tuple = new Tuple<string,string>("jack", "steve");
Console.WriteLine("Value = " + tuple.Item1);
if (tuple.Item1 == "jack") {
Console.WriteLine("Exists: Tuple Value = " +tuple.Item1);
}
if (tuple.Item2 == "david") {
Console.WriteLine("Exists: Tuple Value = " +tuple.Item2);
}
}
}輸出
這將生成以下輸出:
Value = jack Exists: Tuple Value = jack
示例
現在讓我們來看另一個在 C# 中實現 2 元組的示例:
using System;
public class Demo {
public static void Main(string[] args) {
Tuple<int,string> tuple = new Tuple<int,string>(20, "steve");
Console.WriteLine("Value = " + tuple.Item1);
if (tuple.Item1 == 20) {
Console.WriteLine("Exists: Tuple Value = " +tuple.Item1);
}
if (tuple.Item2 == "david") {
Console.WriteLine("Exists: Tuple Value = " +tuple.Item2);
}
}
}輸出
這將生成以下輸出:
Value = 20 Exists: Tuple Value = 20
廣告
資料結構
聯網
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
JavaScript
PHP