C# ToLowerInvariant and ToUpperInvariant Methods

Lowercase and uppercase words

ToLowerInvariant and ToUpperInvariant are available on the string type. They perform lowercasing and uppercase in a slightly different way than ToLower and ToUpper. Words like invariant are confusing. But they simply mean that the results of the method do not depend on the system's culture.

This C# article covers the ToLowerInvariant and ToUpperInvariant methods.

Example

Note

In this example, we see a program that invokes the ToLowerInvariant and ToUpperInvariant instance methods on the string variable. The program shows that the invariant methods act upon the characters in the expected way. In some cases, these invariant methods can be different than other methods because they specify the invariant culture.

Program that uses invariant case methods [C#]

using System;

class Program
{
    static void Main()
    {
	// This demonstrates the invariant methods.
	// ... They act in the expected way.
	string test1 = "Cat";
	Console.WriteLine(test1.ToLowerInvariant());
	Console.WriteLine(test1.ToUpperInvariant());
    }
}

Output

cat
CAT
.NET Framework information

Understanding ToLowerInvariant. What is the meaning of the invariant methods in the .NET Framework? MSDN states that ToLowerInvariant and ToUpperInvariant are useful only for operating system "identifiers" and only affect behavior with specific locales, such as Turkish.

What MSDN documentation says. 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 methods. Look at the documents about ToLowerInvariant, its sister method ToUpperInvariant, and the version that works on the char data type.

MSDN reference 1 MSDN reference 2 MSDN reference 3

Meaning

Programming tip

Invariant methods usually have the same effect as the default methods. In other words, ToLower is very similar in most places to ToLowerInvariant. The documents indicate that these methods will only change behavior with Turkish cultures. Also, on Windows systems, the file system is case-insensitive, which further limits its use.

Summary

The C# programming language

We demonstrated the ToLowerInvariant and ToUpperInvariant methods in general. These invariant methods have a slightly different action in certain cultural contexts, which was not shown here. In most cases, the invariant methods are identical to the regular methods; this is demonstrated in the program text.

String Type
.NET