C# How to Convert String to Uppercase Easily
In C#, you can convert a string to uppercase by using the
ToUpper() method like this: string upper = original.ToUpper();.Examples
Inputhello
OutputHELLO
InputCSharp Programming
OutputCSHARP PROGRAMMING
Input123abc!@
Output123ABC!@
How to Think About It
To convert a string to uppercase, think of changing every letter to its capital form. The
ToUpper() method does this by scanning each character and replacing lowercase letters with their uppercase versions, leaving other characters unchanged.Algorithm
1
Get the input string.2
Call the method that converts all letters to uppercase.3
Return or print the new uppercase string.Code
csharp
using System; class Program { static void Main() { string original = "Hello World!"; string upper = original.ToUpper(); Console.WriteLine(upper); } }
Output
HELLO WORLD!
Dry Run
Let's trace the string "Hello World!" through the code
1
Original string
original = "Hello World!"
2
Convert to uppercase
upper = original.ToUpper() -> "HELLO WORLD!"
3
Print result
Output: HELLO WORLD!
| Step | String Value |
|---|---|
| 1 | Hello World! |
| 2 | HELLO WORLD! |
| 3 | HELLO WORLD! |
Why This Works
Step 1: Use ToUpper() method
The ToUpper() method is built into C# strings and converts all lowercase letters to uppercase.
Step 2: Original string unchanged
Strings are immutable, so ToUpper() returns a new string without changing the original.
Step 3: Non-letter characters stay same
Characters like numbers or symbols are not changed by ToUpper().
Alternative Approaches
Using ToUpperInvariant()
csharp
using System; class Program { static void Main() { string original = "Hello World!"; string upper = original.ToUpperInvariant(); Console.WriteLine(upper); } }
This method converts to uppercase using invariant culture, which is useful for consistent results regardless of user locale.
Using CultureInfo for specific culture
csharp
using System; using System.Globalization; class Program { static void Main() { string original = "Hello World!"; string upper = original.ToUpper(new CultureInfo("tr-TR")); Console.WriteLine(upper); } }
This converts to uppercase using Turkish culture rules, which can affect certain letters like 'i'.
Complexity: O(n) time, O(n) space
Time Complexity
The method scans each character once, so time grows linearly with string length.
Space Complexity
A new string is created for the uppercase result, so space also grows linearly with input size.
Which Approach is Fastest?
ToUpper() and ToUpperInvariant() have similar performance; using culture-specific ToUpper(CultureInfo) may be slightly slower due to culture rules.
| Approach | Time | Space | Best For |
|---|---|---|---|
| ToUpper() | O(n) | O(n) | General uppercase conversion |
| ToUpperInvariant() | O(n) | O(n) | Culture-independent uppercase |
| ToUpper(CultureInfo) | O(n) | O(n) | Culture-specific uppercase rules |
Use
ToUpper() for simple uppercase conversion and ToUpperInvariant() for culture-independent results.Forgetting that strings are immutable and expecting
ToUpper() to change the original string directly.