If you can cache the valid values, I would create a Dictionary<string, HashSet<int>>
and then validate like so:
Dictionary<string, HashSet<int>> validCountryYears; // maybe a field or static field
if (!validCountryYears.TryGetValue(country, out var validYears))
{
throw new ArgumentException(String.Format("{0} isn't a valid country.", country), nameof(country));
}
if (!validYears.Contains(year))
{
throw new ArgumentException(String.Format("{0} isn't valid for the country {1}.", year, country), nameof(year));
}
Try it online
P.S. If you require country
to be case insensitive then you can specify a StringComparer
to the dictionary constructor (e.g. new Dictionary<string, HashSet<int>>(StringComparer.OrdinalIgnoreCase)
)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…