C# 使用索引在列表中查找值? List hello=new List(); 您好,添加(“红色”); 您好。添加(“蓝色”);

C# 使用索引在列表中查找值? List hello=new List(); 您好,添加(“红色”); 您好。添加(“蓝色”);,c#,list,C#,List,如何使用索引搜索此列表以获得红色,即0?我尝试了很多不同的方法,但都失败了。如果我理解你的问题,那么 最好直接使用列表索引器。更地道、更快。 List<String> hello = new List<String>(); hello.Add("red"); hello.Add("blue"); List<String> hello = new List<String>(); hello.Add("red"); hello.Add("blue

如何使用索引搜索此列表以获得
红色
,即
0
?我尝试了很多不同的方法,但都失败了。

如果我理解你的问题,那么


最好直接使用列表索引器。更地道、更快。
List<String> hello = new List<String>();

hello.Add("red");
hello.Add("blue");
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 
var word = hello.ElementAt(0);