C# 程式訪問元組元素


建立一個元組。

var myTuple = Tuple.Create(1, 2.5M, "Amit", "100");

現在要用性質訪問元組元素。

要訪問第一個元素。

myTuple.Item1

要訪問第二個元素。

myTuple.Item2

同樣的,對於其他元素,用法如下:-

示例

 現場演示

using System;
public class Program {
   public static void Main() {
      var myTuple = Tuple.Create(1, 2.5M, "Amit", "100");
      Console.WriteLine("Item1 : "+ myTuple.Item1);
      Console.WriteLine("Item2 : "+ myTuple.Item2);
      Console.WriteLine("Item3 : "+ myTuple.Item3);
      Console.WriteLine("Item4 : "+ myTuple.Item4);
   }
}

輸出

Item1 : 1
Item2 : 2.5
Item3 : Amit
Item4 : 100

更新時間:2020-06-23

276 次瀏覽

開啟你的 職業生涯

完成課程即可獲得認證

開始吧
廣告