Do you have an idea to avoid doing multiple String.Equals? For instance:
if (interSubDir.Equals("de") || interSubDir.Equals("de-DE"))
Thanks!
Do you have an idea to avoid doing multiple String.Equals? For instance:
if (interSubDir.Equals("de") || interSubDir.Equals("de-DE"))
Thanks!
If you are simply trying to make it more readable, or require less typing, you can write a string extension method like so:
public static class StringExt
{
public static bool EqualsAnyOf(this string value, params string[] targets)
{
return targets.Any(target => target.Equals(value));
}
}
Which you can use as follows:
if (interSubDir.EqualsAnyOf("de", "de-DE"))
Or
if (interSubDir.EqualsAnyOf("de", "de-DE", "en", "en-GB", "en-US"))
and so on.
Create collection of values:
string[] values = { "de", "de-DE" };
Use Contains
method:
if (values.Contains(interSubDir))
It gives O(n) performance.
If your collection is very big, then you can use Array.BinarySearch
method, that gives you O(log n) performance.
if (Array.BinarySearch(values, interSubDir) >= 0)
However, the collection must be sorted first.
Array.Sort(values);
Linq could be of use. listToCheckAgainst
could be a simple variable or a private/public property.
var listToCheckAgainst = new[] { "de", "DE-de" };
if(listToCheckAgainst.Any(x => interSubDir.Equals(x)));