让我们看一个列表集合的例子。
我们设置了元素 −
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中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号