I am getting back a "string[]" from a 3rd party library. I want to do a contains on it. what is the most efficient way of doing this?
Array.IndexOf:
bool contains = Array.IndexOf(arr, value) >= 0;
Or just use LINQ:
bool contains = arr.Contains(value);
LINQ should be "fast enough" for most purposes.
If you are only checking a single time, use Array.IndexOf
or the LINQ Contains method like Marc proposed. If you are checking several times, it might be faster to first convert the string array into a HashSet<string>
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With