元組C# 中的類
Tuple<T1, T2, T3> 類表示一個 3 元組,稱為三元組。元組是一種具有元素序列的資料結構。
它用於 −
- 更輕鬆地訪問資料集。
- 更輕鬆地操縱資料集。
- 表示單個數據集。
- 從方法返回多個值
- 向方法傳遞多個值
它有三個屬性 −
Item1 − 獲取當前 Tuple<T1, T2, T3> 物件的第一部分的值。
Item2 − 獲取當前 Tuple<T1, T2, T3> 物件的第二部分的值。
Item3 − 獲取當前 Tuple<T1, T2, T3> 物件的第三部分的值。
示例
現在讓我們看一個在 C# 中實現 3 元組的示例 −
using System;
public class Demo {
public static void Main(string[] args) {
Tuple<int,string,string> tuple = new Tuple<int,string,string>(35, "steve", "katie");
Console.WriteLine("Value (Item1)= " + tuple.Item1);
Console.WriteLine("Value (Item2)= " + tuple.Item2);
Console.WriteLine("Value (Item3)= " + tuple.Item3);
if (tuple.Item1 == 35) {
Console.WriteLine("Exists: Tuple Value = " +tuple.Item1);
}
if (tuple.Item2 == "jack") {
Console.WriteLine("Exists: Tuple Value = " +tuple.Item2);
}
if (tuple.Item3 == "katie") {
Console.WriteLine("Exists: Tuple Value = " +tuple.Item3);
}
}
}輸出
這將產生以下輸出 −
Value (Item1)= 35 Value (Item2)= steve Value (Item3)= katie Exists: Tuple Value = 35 Exists: Tuple Value = katie
示例
現在讓我們看另一個在 C# 中實現 3 元組的示例 −
using System;
public class Demo {
public static void Main(string[] args) {
Tuple<string,string,string> tuple = new Tuple<string,string,string>("nathan", "steve", "katie");
Console.WriteLine("Value (Item1)= " + tuple.Item1);
Console.WriteLine("Value (Item2)= " + tuple.Item2);
Console.WriteLine("Value (Item3)= " + tuple.Item3);
if (tuple.Item1 == "nathan") {
Console.WriteLine("Exists: Tuple Value = " +tuple.Item1);
}
if (tuple.Item2 == "jack") {
Console.WriteLine("Exists: Tuple Value = " +tuple.Item2);
}
if (tuple.Item3 == "katie") {
Console.WriteLine("Exists: Tuple Value = " +tuple.Item3);
}
}
}輸出
這將產生以下輸出 −
Value (Item1)= nathan Value (Item2)= steve Value (Item3)= katie Exists: Tuple Value = nathan Exists: Tuple Value = katie
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
JavaScript
PHP