tags:

views:

71

answers:

1

What's the equivalent of php's in_array or IList.Contains in C#.net. I'm a C n00b but I know very little is buit-in. If I have to do this myself, what's the best way? iterate over the array and test each value?

+9  A: 

There is no way to tell if a given value is in a C array without inspecting the array yourself. If your array is unsorted, you have to look at every element. If you know some more details about the contents of the array, choose your favourite search algorithm and start looking!

Linear search is trivial to implement yourself. Binary search isn't much harder, but it's part of the standard C library already. Check out man bsearch.

Carl Norum