Have Array have Contains method

  • Thread starter Thread starter ad
  • Start date Start date
A

ad

An ArrayList have a Contains method to check if a item in it:
myArrayList.Contains("Jhon");

if if myStringArray is a string array,
How can I check if a item in if
 
ad said:
An ArrayList have a Contains method to check if a item in it:
myArrayList.Contains("Jhon");

if if myStringArray is a string array,
How can I check if a item in if

Use Array.IndexOf and compare the result with -1.
 
The array class doesn't have the equivalent of ArrayList.Contains(). You'll
have to use a for loop or create an enumerator to iterate over the array
items until you find the desired object.

Alternatively, you could also create a temporary array list from the array
like so:

ArrayList _temp = new ArrayList(myArray)
if (_temp.Contains(someObject))
{
// do stuff
}

The latter solution requires less code, but it's much more expensive.
--
Kai Brinkmann [MSFT]

Please do not send e-mail directly to this alias. This alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Duh! That's much faster than what I was suggesting. Only works for a
one-dimensional array, though.
--
Kai Brinkmann [MSFT]

Please do not send e-mail directly to this alias. This alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top