user613326
user613326

Reputation: 2180

Finding matching list item index number c#

I am using .net 2.0 and i try to find the index of a given list. The list is a bit longer but i show it below in a shoter version. I wrote various variation of below code but it doesn't work, although i think i am close to a solution, i am just missing it.

  List<string> arrayLanguages =  new List<string> {"EN","NL","DE"};

  int Languagenr = arrayLanguages.Find(item => item.Equals ("DE"));

Upvotes: 2

Views: 2012

Answers (4)

MethodMan
MethodMan

Reputation: 18863

Why not use the List<T>.FindIndex() Method

List<string> arrayLanguages = new List<string> { "EN", "NL", "DE" };

int Languagenr = arrayLanguages.FindIndex(x=> x== "DE");

Returns Index of 2

Upvotes: 0

Hayden
Hayden

Reputation: 229

There is an IndexOf() function that is inbuilt into List which returns the index value of what you are looking for.

input:

List<string> arrayLanguages = new List<string> { "EN", "NL", "DE" };
int index = arrayLanguages.IndexOf("DE");

output:

2

Upvotes: 0

99823
99823

Reputation: 2457

IndexOf should give you the position

Try this:

int pos = arrayLanguages.IndexOf("EN");

Upvotes: 2

Igor Ralic
Igor Ralic

Reputation: 15006

IndexOf() is what you're looking for!

List<string> arrayLanguages = new List<string> { "EN", "NL", "DE" };

int Languagenr = arrayLanguages.IndexOf("DE");

Upvotes: 3

Related Questions