从 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