从 C# 集合中检索元素
我们来看一个列表集合的例子。
我们已经设置了以下元素 -
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);
}
}
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C #
MongoDB
MySQL
Javascript
PHP