Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Trick when using Array.Contains()

5.00/5 (14 votes)
13 Jun 2011CPOL 14.9K  
The Array class already contains this capability with no casting required. The IndexOf method is a static one that returns the index of the value and a -1 if it doesn't exist.Here is an example:exists = Array.IndexOf(arr, item) >= 0

The Array class already contains this capability with no casting required. The IndexOf method is a static one that returns the index of the value and a -1 if it doesn't exist.


Here is an example:


C#
exists = Array.IndexOf(arr, "item") >= 0

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)