C# 中的元組類

王林
發布: 2023-08-29 13:33:05
轉載
1417 人瀏覽過

C# 中的元组类

Tuple類別表示一個4元組,稱為四元組。元組是一種具有元素序列的資料結構。

它用於 -

  • 更輕鬆地存取資料集。
  • 更輕鬆地操作資料集。
  • 表示單一資料集。
  • 從方法傳回多個值
  • 傳遞多個值到一個方法

#它有四個屬性-

  • ##Item1 - 取得目前Tuple的值 物件的第一個元件。

  • Item2 - 取得目前 Tuple 的值物件的第二個元件。

  • Item3 - 取得目前 Tuple 物件的第三個元件的值。

  • Item4 - 取得目前 Tuple 物件的第四個元件的值。

  • ul>範例

    現在讓我們來看一個在C# 中實作4 元組的範例-

    using System;
    public class Demo {
       public static void Main(string[] args) {
          Tuple<string,string,string,string> tuple = new Tuple<string,string,string,string>("nathan", "steve", "katie", "tim");
          Console.WriteLine("Value (Item1)= " + tuple.Item1);
          Console.WriteLine("Value (Item2)= " + tuple.Item2);
          Console.WriteLine("Value (Item3)= " + tuple.Item3);
          Console.WriteLine("Value (Item4)= " + tuple.Item4);
          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);
          }
          if (tuple.Item4 == "tom") {
             Console.WriteLine("Exists: Tuple Value = " +tuple.Item4);
          }
       }
    }
    登入後複製

    輸出

    這將產生以下輸出-

    Value (Item1)= nathan
    Value (Item2)= steve
    Value (Item3)= katie Value
    Value (Item4)= tom
    Exists: Tuple Value = nathan
    Exists: Tuple Value = katie
    登入後複製

    範例

    現在讓我們看另一個在C# 中實作4 元組的範例-

    using System;
    public class Demo {
       public static void Main(string[] args) {
          Tuple<int,int,int,int> tuple = new Tuple<int,int,int,int>(100, 150, 300, 450);
          Console.WriteLine("Value (Item1)= " + tuple.Item1);
          Console.WriteLine("Value (Item2)= " + tuple.Item2);
          Console.WriteLine("Value (Item3)= " + tuple.Item3);
          Console.WriteLine("Value (Item4)= " + tuple.Item4);
          if (tuple.Item1 == 100) {
             Console.WriteLine("Exists: Tuple Item 1 = " +tuple.Item1);
          }
          if (tuple.Item2 == 250) {
             Console.WriteLine("Exists: Tuple Item 2 = " +tuple.Item2);
          }
          if (tuple.Item3 == 270) {
             Console.WriteLine("Exists: Tuple Item 3 = " +tuple.Item3);
          }
          if (tuple.Item4 == 300) {
             Console.WriteLine("Exists: Tuple Item 4 = " +tuple.Item4);
          }
       }
    }
    登入後複製

    輸出

    這將產生以下輸出-

    Value (Item1)= 100
    Value (Item2)= 150
    Value (Item3)= 300
    Value (Item4)= 450
    Exists: Tuple Item 1 = 100
    登入後複製

以上是C# 中的元組類的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!