1 out of 2 rated this helpful - Rate this topic

String.ToUpperInvariant Method

Returns a copy of this String object converted to uppercase using the casing rules of the invariant culture.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
public string ToUpperInvariant()

Return Value

Type: System.String
The uppercase equivalent of the current string.

The invariant culture represents a culture that is culture-insensitive. It is associated with the English language but not with a specific country or region. For more information, see Using the InvariantCulture Property.

If your application depends on the case of a string changing in a predictable way that is unaffected by the current culture, use the ToUpperInvariant method. The ToUpperInvariant method is equivalent to ToUpper(CultureInfo.InvariantCulture). The method is recommended when a collection of strings must appear in a predictable order in a user interface control.

Note Note

   This method does not modify the value of the current instance. Instead, it returns a new string in which all characters in the current instance are converted to uppercase.

Security Considerations

The casing operation that results from calling the ToLower method takes the casing conventions of the current culture into account. If you need the lowercase or uppercase version of an operating system identifier, such as a file name, named pipe, or registry key, use the ToLowerInvariant or ToUpperInvariant method. This produces the same result in every culture (unlike the ToLower method) and performs more efficiently.

The following example defines a string array that contains a single word in a number of languages. The ToUpperInvariant method is used to populate the elements of a parallel array with the case-insensitive version of each word. The Array.Sort<TKey, TValue>(TKey[], TValue[], IComparer<TKey>) method is used to sort the case-sensitive array based on the order of elements in the uppercase array to ensure that elements appear in the same order regardless of language.


using System;
using System.IO;

public class Example
{
   public static void Main()
   {
      string[] words = { "Tuesday", "Salı", "Вторник", "Mardi", 
                         "Τρίτη", "Martes", "יום שלישי", 
                         "الثلاثاء", "วันอังคาร" };
      StreamWriter sw = new StreamWriter(@".\output.txt");

      // Display array in unsorted order.
      foreach (string word in words)
         sw.WriteLine(word);

      sw.WriteLine();

      // Create parallel array of words by calling ToUpperInvariant.
      string[] upperWords = new string[words.Length];
      for (int ctr = words.GetLowerBound(0); ctr <= words.GetUpperBound(0); ctr++)
         upperWords[ctr] = words[ctr].ToUpperInvariant();

      // Sort the words array based on the order of upperWords.
      Array.Sort(upperWords, words, StringComparer.InvariantCulture);

      // Display the sorted array.
      foreach (string word in words)
         sw.WriteLine(word);

      sw.Close();      
   }
}
// The example produces the following output:
//       Tuesday
//       Salı
//       Вторник
//       Mardi
//       Τρίτη
//       Martes
//       יום שלישי
//       الثلاثاء
//       วันอังคาร
//       
//       Mardi
//       Martes
//       Salı
//       Tuesday
//       Τρίτη
//       Вторник
//       יום שלישי
//       الثلاثاء
//       วันอังคาร


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
More simple example with outputs
Sample is not very clear, here is one more explicit :


var text = "Français accentué"; // french with accent
Console.WriteLine("Original: "+ text); // orignal text
Console.WriteLine("ToUpper(): "+text.ToUpper()); // upper text, will keep the accent and special ç letter
Console.WriteLine("ToUpperInvariant(): "+text.ToUpperInvariant()); // will have the same effect
Console.WriteLine("ToLower: "+text.ToLower()); // will keep accent and special letter
Console.WriteLine("ToLowerInvariant: "+text.ToLowerInvariant()); // the same
Console.WriteLine("ToLower with US culture: "+text.ToLower(new System.Globalization.CultureInfo("en-US"))); // no change
// to make lower or upper text without accent and national special characters :
byte[] bString =
System.Text.Encoding.GetEncoding(1251).GetBytes(text);
string stringWithoutAccent = System.Text.Encoding.ASCII.GetString(bString);
Console.WriteLine("Without accent: "+stringWithoutAccent); // original with no more accent or special char
Console.WriteLine("Upper without Accent: " + stringSansAccent.ToUpper()); // upper cas with no accent nor special char

Output will be :

Original: Français accentué
ToUpper(): FRANÇAIS ACCENTUÉ
ToUpperInvariant(): FRANÇAIS ACCENTUÉ
ToLower: français accentué
ToLowerInvariant: français accentué
ToLower with US culture: français accentué
Without accent: Francais accentue
Upper without Accent: FRANCAIS ACCENTUE