This article illustrates the different techniques to transform a string to uppercase and lowercase in C#.

1. Using String.ToUpper() method

A simple solution to transform a string to uppercase in C# is to invoke the String.ToUpper() instance method on your string. Since the string is immutable in C#, this method returns a copy of the string with each character converted to uppercase. The following example shows the working of the String.ToUpper() method by converting a given string to its uppercase equivalent:

Download  Run Code

2. Using String.ToLower() method

Similarly, to get the lowercase equivalent of a string, you can use String.ToLower() method. Like String.ToUpper() method, String.ToLower() returns a new string with all characters converted to lowercase. You can use either String.ToUpper() or String.ToLower() method to transform a string for case-insensitive comparison.

Download  Run Code

3. Invert Case

If you need to invert the case of all characters in the string, you can invoke the Char.ToUpper() or Char.ToLower() method on each character of the string. This logic is demonstrated below using LINQ’s Select() method:

Download  Run Code

 
Note that the above solution needs the System.Linq namespace. Instead of using LINQ’s Select() method, you can also use the foreach loop to traverse the string and invert the case of each character.

Download  Run Code

That’s all about transforming a string to uppercase and lowercase in C#.