ChaimG
ChaimG

Reputation: 7514

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

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

Upvotes: 5

Views: 9713

Answers (3)

Dotnetter
Dotnetter

Reputation: 261

use FindIndex:

    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" };
    var index = Markets.FindIndex(a => a == "HKG");

Upvotes: 1

Sajeetharan
Sajeetharan

Reputation: 222522

use IndexOf

int index = Markets.IndexOf("HKG");

Upvotes: 10

Ming-Tang
Ming-Tang

Reputation: 17651

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

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

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

Upvotes: 9

Related Questions