Example. Here you see that char.ToLower converts uppercase characters to lowercase characters, while not changing characters that are already lowercase or digits. In the same example, you see char.ToUpper, which is the opposite.
Main: The 4 calls to char.ToLower show how this static method is used. The comments show the values returned by char.ToLower.
Next: The 4 calls to char.ToUpper show how this method is used. The result from each call is shown.
StaticFinally: The last statement in the code example is the most complex and it is a call to WriteLine with a format string.
Consolestring.Format C# program that uses char.ToLower
using System;
class Program
{
static void Main()
{
char c1 = 'a'; // Lowercase a
char c2 = 'b'; // Lowercase b
char c3 = 'C'; // Uppercase C
char c4 = '3'; // Digit 3
char lower1 = char.ToLower(c1); // a
char lower2 = char.ToLower(c2); // b
char lower3 = char.ToLower(c3); // c [changed]
char lower4 = char.ToLower(c4); // 3
char upper1 = char.ToUpper(c1); // A [changed]
char upper2 = char.ToUpper(c2); // B [changed]
char upper3 = char.ToUpper(c3); // C
char upper4 = char.ToUpper(c4); // 3
Console.WriteLine("{0},{1},{2},{3}\n" +
"{4},{5},{6},{7}\n" +
"{8},{9},{10},{11}",
c1, c2, c3, c4,
lower1, lower2, lower3, lower4,
upper1, upper2, upper3, upper4);
}
}
Output
a,b,C,3
a,b,c,3
A,B,C,3
Discussion. Sometimes it is better to first test if a character is lowercase or uppercase. Fortunately, the .NET Framework provides the char.IsLower and char.IsUpper methods, which also alias the System.Char type and are static methods.
char.IsLowerTip: These four static methods use value semantics, which means the parameter is copied to the method as a value.
Therefore: The ToLower and ToUpper methods do not modify the variable you use—they return a new value.