Export (0) Print
Expand All
1 out of 1 rated this helpful - Rate this topic

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.

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

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.

static object LookupKey(string key) 
{
    return internalHashtable[key.ToLower(CultureInfo.InvariantCulture)];
}

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 Turkish (Turkey) and Azerbaijani (Latin, Azerbaijan). 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, it is recommended that you always use the overloads of these methods that allow you to explicitly specify a culture parameter.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.