C# ToLowerInvariant

Lowercase and uppercase words

ToLowerInvariant and ToUpperInvariant modify strings. They lowercase and uppercase differently than ToLower and ToUpper. The word invariant is confusing. It means the system's culture has no effect on the result.

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.

And:In some cases, these invariant methods can be different from other methods because they specify the invariant culture.

C# program that uses invariant case methods

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
Framework: NET

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.

ToLowerInvariant Method: MSDNToUpperInvariant Method: MSDN

Also:The char type offers a ToLowerInvariant method, which has the same effect for a char.

Char.ToLowerInvariant Method: MSDN

Discussion

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.

Tip: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.

Review:In most cases, the invariant methods are identical to the regular methods. This is demonstrated in the program text.


C#: String