I have an array of valid string values like this:
string[] validValues = new string[] {"none","all","some"};
I have a method that validates a user's selection in a drop down box like this:
bool valid = validValues.Contains( selection );
I don't want to re-create the validValues
array every time this method is called, so I'd like to assign it to a static readonly
property. The array itself is immutable. Is this scenario thread-safe? This is part of an MVC controller method, so it's quite possible that multiple threads will be accessing the array via the Enumerable<string>.Contains
method at the same time.
If the enumeration is not thread safe, then would it be thread safe to use a method like Array.IndexOf
instead? Decompiling shows that the method either calls the native method TrySZIndexOf, or it iterates over the array by index. From what I've read here, that may be safe? I'd just rather use Contains because it conveys what's trying to be accomplished more clearly than IndexOf() == -1
.