Skip to content
Tutorials Center
My learning

Topics

C# Reference · Strings and Text

C# ToUpper() and ToLower() methods

Reference for C# ToUpper(), ToLower(), ToUpperInvariant() and ToLowerInvariant(): case conversion, culture pitfalls, the Turkish i problem and title case.

ToUpper and ToLower return a copy of a string with every letter converted to upper or lower case. Characters that have no case, such as digits and punctuation, are left alone. The original string is not changed.

There are two families. ToUpper() and ToLower() use the current culture's casing rules. ToUpperInvariant() and ToLowerInvariant() use fixed, culture-independent rules. For identifiers, keys and protocol text you almost always want the invariant versions, or better, a case-insensitive comparison that avoids creating new strings at all.

C#
string a = "Hello".ToUpperInvariant();  // "HELLO"
string b = "Hello".ToLowerInvariant();  // "hello"

Try it

C# Changing case

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Compare without changing case, and limits of ToUpper

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
public string ToUpper()
public string ToUpper(CultureInfo? culture)
public string ToUpperInvariant()

public string ToLower()
public string ToLower(CultureInfo? culture)
public string ToLowerInvariant()
Name Description
culture The culture whose casing rules apply. null means the current culture.

Returns: a new string, or the same instance if nothing needed to change.

Errors / undefined behavior: none for non-null strings. A culture argument that is null is treated as the current culture, not as an error.

Usage notes

  • The Turkish i problem. In Turkish and Azerbaijani cultures, "i".ToUpper() gives a dotted capital İ and "I".ToLower() gives a dotless ı. Code that lowercases a keyword like "TITLE" and then compares it to "title" fails on those machines. The invariant methods always follow the same rules, so they are safe for protocol text. Our runner uses invariant globalization, so culture-specific behavior cannot be shown live here; treat other cultures as read-only knowledge.
  • Prefer a comparison to a conversion. string.Equals(a, b, StringComparison.OrdinalIgnoreCase), StringComparer.OrdinalIgnoreCase for dictionaries and sets, and Contains or IndexOf with a comparison type all avoid allocating lowercase copies.
  • Normalizing for storage. If you must store a normalized key, use ToUpperInvariant(). Microsoft guidance favors upper case because a few characters round-trip better that way.
  • No title-case method on string. TextInfo.ToTitleCase exists but follows culture data and does not always give what people expect (it leaves words that are all upper case alone, for instance). For simple cases, write a small helper.
  • Per-character mapping. The conversions work character by character. They do not turn ß into SS, because the result must have the same length. Full Unicode case folding needs other tools.
  • Spans. ReadOnlySpan<char>.ToUpperInvariant(Span<char>) in System.MemoryExtensions converts without allocating a string.
  • Do not use case conversion for password or token comparison. Compare secrets with a constant-time method, not by normalizing and testing equality.