2013-02-13 53 views
0
List<String> hello = new List<String>(); 

hello.Add("red"); 
hello.Add("blue"); 

如何使用索引搜索此列表得到"red"这将是0?我尝试了很多不同的方法,但都失败了。使用索引在List中查找值?

回答

2

如果我明白你的问题,该.FindIndex Method

List<String> hello = new List<String>(); 

hello.Add("red"); 
hello.Add("blue"); 

var index = hello.FindIndex(c => c == "red"); 
var word = hello[index]; //<-- get the word 

,最后用.ElementAt Method

var word = hello.ElementAt(0); 
+1

更好地直接使用列表索引。更习惯和更快。 – usr 2013-02-14 10:02:32