How to Convert a String to Uppercase with ToUpperInvariant in C#
text.ToUpperInvariant() returns an uppercase copy of a string, converted with the same rules on every computer. Use it for codes, keys and anything you compare or store in one standard case.
Microsoft's reference for String.ToUpperInvariant: Returns a copy of this String object converted to uppercase using the casing rules of the invariant culture. A culture is a set of language rules, and the computer's current culture can change how a letter is converted. The invariant culture is a fixed one: 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. Microsoft's guide to comparing strings: ToUpperInvariant is the standard way to normalize case.
Syntax
string <upper> = <text>.ToUpperInvariant();
| Part | What it is |
|---|---|
<text> | The string to convert |
ToUpperInvariant() | Converts every letter to uppercase with the invariant culture's rules, the same on every computer |
<upper> | A new string in uppercase. <text> itself doesn't change |
The Example
A console app called ToUpperInvariant takes five bits of text as people might type them: three airport codes in different cases, a gate number with digits, and a word with an accent. It converts each one with ToUpperInvariant and prints it next to the original.
Prerequisites
- The .NET 10 SDK.
dotnet --versionprints a version that starts with10.0.
Step 1: Create a Console App
dotnet new console -n ToUpperInvariant
cd ToUpperInvariant
The template "Console App" was created successfully.
Processing post-creation actions...
Restoring /Users/you/source/ToUpperInvariant/ToUpperInvariant.csproj:
Determining projects to restore...
Restored /Users/you/source/ToUpperInvariant/ToUpperInvariant.csproj (in 40 ms).
Restore succeeded.
dotnet new console -n ToUpperInvariant creates a folder named ToUpperInvariant with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or ToUpperInvariant.csproj in Visual Studio.
Step 2: Write the Code
Replace everything in Program.cs. First the text as it was typed:
// Codes and words as people typed them.
string[] typed = ["lhr", "Jfk", "SYD", "gate b12", "café"];
typed is an array of five strings, some in lowercase, some in uppercase and some mixed.
Then convert each one:
foreach (string text in typed)
{
// Uppercase with the same rules on every computer.
string upper = text.ToUpperInvariant();
Console.WriteLine($"{text,-8} -> {upper}");
}
text.ToUpperInvariant() returns an uppercase copy of text, which the loop keeps in upper. ,-8 pads the original to 8 characters, so the arrows line up.
Step 3: Run It
Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:
dotnet run
lhr -> LHR
Jfk -> JFK
SYD -> SYD
gate b12 -> GATE B12
café -> CAFÉ

Every letter came out in uppercase: lhr and Jfk became LHR and JFK, and SYD, already in uppercase, stayed SYD. In gate b12, the space and the digits stayed as they were: GATE B12. The accented é became É, in CAFÉ.
The Complete File

// Codes and words as people typed them.
string[] typed = ["lhr", "Jfk", "SYD", "gate b12", "café"];
foreach (string text in typed)
{
// Uppercase with the same rules on every computer.
string upper = text.ToUpperInvariant();
Console.WriteLine($"{text,-8} -> {upper}");
}
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Using ToUpper() where the result has to be the same everywhere | With the current culture set to Turkish, "title".ToUpper() returned TİTLE, with a dotted İ, so comparing it with "TITLE" gave False. The ToUpper() reference: This method uses the casing rules of the current culture to convert each character in the current instance to its uppercase equivalent. | Use ToUpperInvariant(). Under the same Turkish culture, it returned TITLE, and the comparison gave True |
| Expecting the string itself to change | code.ToUpperInvariant(); on a line of its own left code as lhr. The reference: 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. | Keep the result: after code = code.ToUpperInvariant();, it printed LHR |
Calling it on a string that may be null | nickname.ToUpperInvariant() built with warning CS8602: Dereference of a possibly null reference., then stopped the program: Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object. | nickname?.ToUpperInvariant() ?? "(no nickname)" printed (no nickname): ?. skips the call when the string is null, and ?? gives a value to use instead |
FAQ
How do I make only the first letter uppercase?
Convert the first character with char.ToUpperInvariant, and add the rest of the string:
string city = "paris";
string title = char.ToUpperInvariant(city[0]) + city[1..];
Console.WriteLine(title);
Paris
city[0] is the first character, and city[1..] is the rest of the string. The Char.ToUpperInvariant reference: Converts the value of a Unicode character to its uppercase equivalent using the casing rules of the invariant culture.
Is ToUpperInvariant the same as ToUpper with the invariant culture?
Yes. The reference: The ToUpperInvariant method is equivalent to ToUpper(CultureInfo.InvariantCulture). ToUpperInvariant() is just shorter, and it needs no using System.Globalization; line for CultureInfo.
Conclusion
ToUpperInvariant() returns an uppercase copy of a string, converted with the invariant culture's rules, so the result is the same on every computer. Keep the result, because the original doesn't change, guard against null with ?., and use char.ToUpperInvariant when only one character should change.
Additional Resources
String.ToUpperInvariantMethod — the method and the invariant culture.String.ToUpperMethod — uppercase with the current culture's rules, or a culture you choose.Char.ToUpperInvariantMethod — one character in uppercase.- Best practices for comparing strings in .NET — when to change case, and when to compare without it.
