0

Is there a built in way to do this or do i have to write my own loop? I have tried the code below:

int index = Array.IndexOf(options, ind => ind.Contains("COM"));

But it gives the following error: can not convert lambda to type object. This code runs but always returns -1:

int index = Array.IndexOf(options, options.Where(ind => ind.Contains("COM")));

Any help is welcome, thanks

2 Answers2

1

Try this:

int index = Array.FindIndex(options, s => s.StartsWith("COM"));
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
0

You can use Array.FindIndex(T):

string[] foo = { "FOO", "BAR", "COM", "BUZZ" };
int theIndex = Array.FindIndex(foo, s => s.StartsWith("COM"));
// returns 2
DVK
  • 2,726
  • 1
  • 17
  • 20