首頁  >  文章  >  後端開發  >  在C#中檢索集合中的元素

在C#中檢索集合中的元素

WBOY
WBOY轉載
2023-08-21 20:45:021519瀏覽

讓我們來看一個列表集合的例子。

我們設定了元素 −

List<int> list = new List<int>();
list.Add(20);
list.Add(40);
list.Add(60);
list.Add(80);

現在假設我們需要從清單中檢索第一個元素。為此,設定索引如下−

int a = list[0];

以下是一個範例,展示如何從清單集合中檢索元素 −

範例

using System;
using System.Collections.Generic;

class Demo {

   static void Main(String[] args) {
      List<int> list = new List<int>();
      list.Add(20);
      list.Add(40);
      list.Add(60);
      list.Add(80);

      foreach (int val in list) {
         Console.WriteLine(val);
      }

      int a = list[0];
      Console.WriteLine("First element: "+a);
   }
}

以上是在C#中檢索集合中的元素的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除