One would think that the optimizer in the C# and/or the JIT compilers would have the smarts to recognize a loop-invariant expression and refactor outside of the loop. My inclination is to do such refactorings myself as is makes the code clearer.
Even better, use this method:
CultureInfo ci = CultureInfo.GetCultureInfo("en-US") ;
It gives you a cached, read-only instance, the instance will only be constructed once and thence retrieve from cache.
Better yet, for your stated purposes:
This [CultureInfo
] object is required in many Date and String methods
to force a specific culture when CurrentCulture may
not be the right one.
use CultureInfo.InvariantCulture
. That is what it exists for.
A third option, would be to create a static CultureInfo
property holding a singleton reference to your fallback culture. Depending on your purposed, you might want to mark it as thread-local (static
methods of CultureInfo
are thread-safe; instance methods are not). Such a propery might look something like this:
public static string FallbackCultureId { get { return Configuration.AppSettings["FallbackConfigurationId"] ; } }
public static CultureInfo FallbackCultureInfo
{
get { return fallBackCultureInfo ?? (fallBackCultureInfo=new CultureInfo(FallbackCultureId)) ; }
}
[ThreadStatic] private static CultureInfo fallBackCultureInfo ;