C#: String

.NET Array Collections File String Async Cast DataTable DateTime Dictionary Enum Exception For Foreach IEnumerable If IndexOf Lambda LINQ List Parse Path Process Property Regex Replace Sort Split StringBuilder Substring Switch Tuple

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

Letters, grey area

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.

Based on:

.NET 4.5

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, the file system is case-insensitive, which further limits its use.

Summary

C# programming language

ToLowerInvariant, and its upper equivalent, are confusing when we first encounter them. 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.