It is associated with the English
language but not with a country or region.
The CultureInfo.InvariantCulture property
is neither a neutral nor a specific culture. It is a third type of
culture that is culture-insensitive. You can use InvariantCulture in
almost any method in the System.Globalization namespace
that requires a culture.
When should we use it?
- You should use the invariant culture only for processes that require culture-independent results, such as system services. In other cases, it produces results that might be linguistically incorrect or culturally inappropriate.
- You should also use InvariantCulture when a security decision will be made based on the result of a string comparison or case change.
The default implementations of methods such
as String.Compare, String.ToUpper,
and String.ToLower use
the CultureInfo.CurrentCulture property.
When we shouldn't use it?
- Code that performs culture-sensitive string operations can cause security vulnerabilities.
- if CultureInfo.CurrentCulture is changed or if the culture on the computer on which the code runs differs from the culture the developer used to test the code, the expected behavior of a string operation will differ from the actual behavior on the computer running that operation.
Best Practices
For more information about using the InvariantCulture property
to perform culture-insensitive string operations, see Culture-InsensitiveString Operations.
Comments