Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find an index of a string within a List in .Net 2.0

Tags:

c#

I am trying to get the index number of a string within a List. I tried the following code:

List<string> Markets = new List<string>() {"HKG", "TYO", "NSE", "STU", "FRA", "LON", "SIN", "MIL", "TSE", "ASX", "STO", "AEX", "MEX", "NSE", "EPA", "SWX", "CVE", "BRU", "SWX"};
int index = Markets.FindIndex("HKG");

The following errors came up:

The best overloaded method match for 'System.Collections.Generic.List.FindIndex(System.Predicate<string>)' has some invalid arguments Argument 1: cannot convert from 'string' to 'System.Predicate<string>'

Any idea how to get the compiler to like this code?

P.S. I am using QuantShare's C# which is supposed to be .Net 2.0

like image 373
ChaimG Avatar asked Nov 14 '14 05:11

ChaimG


2 Answers

use IndexOf

int index = Markets.IndexOf("HKG");
like image 63
Sajeetharan Avatar answered Sep 30 '22 04:09

Sajeetharan


Since it's a predicate, you should plug in a lambda function.

FindIndex(x => x == "HKG")

See: http://www.dotnetperls.com/lambda

like image 28
Ming-Tang Avatar answered Sep 30 '22 04:09

Ming-Tang