다음을 통해 공유


Performing Culture-Insensitive Case Changes 

The String.ToUpper, String.ToLower, Char.ToUpper, and Char.ToLower methods provide overloads that do not accept any parameters. By default, these overloads without parameters perform case changes based on the value of the CultureInfo.CurrentCulture. This produces case-sensitive results that can vary by culture. To make it clear whether you want case changes to be culture-sensitive or culture-insensitive, you should use the overloads of these methods that require you to explicitly specify a culture parameter. For culture-sensitive case changes, specify CultureInfo.CurrentCulture for the culture parameter. For culture-insensitive case changes, specify CultureInfo.InvariantCulture for the culture parameter.

Often, strings are converted to a standard case to enable easier lookup later. When strings are used in this way, you should specify CultureInfo.InvariantCulture for the culture parameter, because the value of Thread.CurrentCulture can potentially change between the time that the case is changed and the time that the lookup occurs.

If a security decision is based on a case change operation, the operation should be culture-insensitive to ensure that the result is not affected by the value of CultureInfo.CurrentCulture. See Custom Case Mappings and Sorting Rules for an example that demonstrates how culture-sensitive string operations can produce inconsistent results.

Using the String.ToUpper and String.ToLower Methods

For code clarity, it is recommended that you always use overloads of the String.ToUpper and String.ToLower methods that allow you to specify a culture parameter explicitly. For example, the following code performs an identifier lookup. The key.ToLower operation is culture-sensitive by default, but this behavior is not clear from reading the code.

Example

Shared Function LookupKey(key As String) As Object
   Return internalHashtable(key.ToLower())
End Function
static object LookupKey(string key) 
{
    return internalHashtable[key.ToLower()];
}

If you want the key.ToLower operation to be culture-insensitive, you should change the preceding example as follows to explicitly use the CultureInfo.InvariantCulture when changing the case.

Shared Function LookupKey(key As String) As Object
    Return internalHashtable(key.ToLower(CultureInfo.InvariantCulture))
End Function
static object LookupKey(string key) 
{
    return internalHashtable[key.ToLower(CultureInfo.InvariantCulture)];
}

Using the Char.ToUpper and Char.ToLower Methods

Although the Char.ToUpper and Char.ToLower methods have the same characteristics as the String.ToUpper and String.ToLower methods, the only cultures that are affected are tr-TR for Turkish (Turkey) and az-Latn-AZ for Azeri (Azerbaijan, Latin). These are the only two cultures with single-character casing differences. For more details about this unique case mapping, see Custom Case Mappings and Sorting Rules. For code clarity and to ensure consistent results, you should always use the overloads of these methods that allow you to explicitly specify a culture parameter.

See Also

Reference

String.ToUpper Method
String.ToLower Method
Char.ToUpper Method
Char.ToLower Method

Other Resources

Performing Culture-Insensitive String Operations