C# – How to search a string in String array


I need to search a string in the string array. I dont want to use any for looping in it

string [] arr = {"One","Two","Three"};

string theString = "One"

I need to check whether theString variable is present in arr.

Best Solution

Well, something is going to have to look, and looping is more efficient than recursion (since tail-end recursion isn't fully implemented)... so if you just don't want to loop yourself, then either of:

bool has = arr.Contains(var); // .NET 3.5


bool has = Array.IndexOf(arr, var) >= 0;

For info: avoid names like var - this is a keyword in C# 3.0.