I have this code, when I try to get not existed culture I get exception.
Is there exists method like TryGetCultureInfo
, which return bool
value? I don't want to use try-catch
statement
CultureInfo culture = CultureInfo.GetCultureInfo(cultureCode);
if (culture == null)
{
culture = CultureInfo.GetCultureInfo(DefaultCultureCode);
}
You could write a DoesCultureExist method returning a boolean value just like this:
private static bool DoesCultureExist(string cultureName)
{
return CultureInfo.GetCultures(CultureTypes.AllCultures).Any(culture => string.Equals(culture.Name, cultureName, StringComparison.CurrentCultureIgnoreCase));
}