tags:

views:

79

answers:

2

Hi,

I have a straight forward string array, why would IndexOf be unavailable? I'm targeting .Net 3.5

Thanks

+2  A: 

Are you talking about the Array.IndexOf method:

var array = new[] { "a", "b", "c" };
Console.WriteLine(Array.IndexOf(array, "b"));
Darin Dimitrov
Yes thanks, I was using (MyArrayName.IndexOf("test"));
Jade M
+1  A: 

Probably because you are assuming it is an instance method instead of a static method:

using System;

class Program {
  static void Main(string[] args) {
    string[] arr = new string[] { "hello", "world" };
    int index = Array.IndexOf(arr, "world");
    // Not:
    //int index = arr.IndexOf("world");
    System.Diagnostics.Debug.Assert(index == 1);
  }
}

Consider List<string> instead of an array, List<>.IndexOf() is an instance method.

Hans Passant