C# 通过索引获取列表项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15456845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting a list item by index
提问by user1909486
I've recently started using c# moving over from Java. I can't seem to find how to get a list item by index. In java to get the first item of the list it would be:
我最近开始使用从 Java 转移过来的 c#。我似乎无法找到如何通过索引获取列表项。在java中获取列表的第一项将是:
list1.get(0);
What is the equivalent in c#?
c# 中的等价物是什么?
回答by Mitch Wheat
list1[0];
Assuming list's type has an indexer defined.
假设列表的类型定义了一个索引器。
回答by user3004826
You can use the ElementAt extension method on the list.
您可以使用列表中的 ElementAt 扩展方法。
For example:
例如:
// Get the first item from the list
using System.Linq;
var myList = new List<string>{ "Yes", "No", "Maybe"};
var firstItem = myList.ElementAt(0);
// Do something with firstItem
回答by Zeyad Qunees
Visual Basic, C#, and C++ all have syntax for accessing the Item property without using its name. Instead, the variable containing the List is used as if it were an array.
Visual Basic、C# 和 C++ 都具有无需使用其名称即可访问 Item 属性的语法。相反,包含 List 的变量被用作数组。
List[index]
See for instance: https://msdn.microsoft.com/en-us/library/0ebtbkkc(v=vs.110).aspx
参见例如:https: //msdn.microsoft.com/en-us/library/0ebtbkkc(v=vs.110).aspx
回答by Xellarant
Old question, but I see that this thread was fairly recently active, so I'll go ahead and throw in my two cents:
老问题,但我看到这个线程最近很活跃,所以我会继续投入我的两分钱:
Pretty much exactly what Mitch said. Assuming proper indexing, you can just go ahead and use square bracket notation as if you were accessing an array. In addition to using the numeric index, though, if your members have specific names, you can often do kind of a simultaneous search/access by typing something like:
米奇说的差不多。假设索引正确,您可以继续使用方括号表示法,就像访问数组一样。但是,除了使用数字索引之外,如果您的成员具有特定名称,您通常可以通过键入以下内容来同时进行搜索/访问:
var temp = list1["DesiredMember"];
The more you know, right?
你知道的越多,对吧?
回答by picolino
.NET List
data structure is an Array
in a "mutable shell".
So you can use indexes for accessing to it's elements like:
因此,您可以使用索引来访问它的元素,例如:
var firstElement = myList[0];
var secondElement = myList[1];
Starting with C# 8.0you can use Index
and Range
classes for accessing elements. They provides accessing from the end of sequence or just access a specific part of sequence:
从C# 8.0开始,您可以使用Index
和Range
类来访问元素。它们提供从序列末尾访问或仅访问序列的特定部分:
var lastElement = myList[^1]; // Using Index
var fiveElements = myList[2..7]; // Using Range, note that 7 is exclusive
You can combine indexes and ranges together:
您可以将索引和范围组合在一起:
var elementsFromThirdToEnd = myList[2..^0]; // Index and Range together
Also you can use LINQ ElementAt
method but for 99% of cases this is really not necessary and just slow performance solution.
您也可以使用 LINQElementAt
方法,但对于 99% 的情况,这确实没有必要,只是性能缓慢的解决方案。
回答by Codemaker
you can use index to access list elements
您可以使用索引来访问列表元素
List<string> list1 = new List<string>();
list1[0] //for getting the first element of the list