Regardless of how you do it, at some point in the execution of a method like that, there will be a enumeration.
What you can do, is make the method more terse by using a LINQ method:
return suffixes.Any(s => value.EndsWith(s));
But is that any more readable than:
foreach (string suffix in suffixes) {
if (value.EndsWith(suffix)) return true;
}
return false;
For actual method improvements, you should consider argument validation. What happens if value is null
? Your current code will return in a NullReferenceException
.
Behaviourly would it be correct to throw an ArgumentNullException
in this case? Or is the possible null
value an automatic resultant value of false?
You should also consider how your are comparing strings? Should you provide an overload that allows the caller to pass in an appropriate CultureInfo
object? Or perhaps specify the StringComparison
?