The Char.ToLowerInvariant(Char) method in C# leverages the invariant culture to convert a given Unicode character to its lowercase form. This functionality is a component of the Char class and proves to be very useful for character manipulation independent of cultural configurations.
Syntax:
It has the following syntax:
public static char ToLowerInvariant(char c);
Access Modifier:
The public access modifier can be utilized to define the visibility of a method. When a method is marked as public, it allows access from both within the same assembly and from external assemblies.
Static Keyword :
Static: The term "static" signifies that the method is associated with the type itself, rather than a specific instance of the type. This means that the method can be invoked without the need to instantiate the class in which it is defined.
Return Type:
char: Char represents the data type of the value that the function gives back. In this scenario, the function yields a solitary Unicode character.
Method Name:
ConvertToLowerInvariant: The method known as ToLowerInvariant is designed to transform a Unicode character into its lowercase form while utilizing the invariant culture.
Example 1:
Let's consider a scenario to demonstrate the Char.ToLowerInvariant(Char) function in C#.
using System;
class Demo
{
static void Main()
{
char original_Char1 = 'S';
char lowercase_Char1 = Char.ToLowerInvariant(original_Char1);
Console.WriteLine($"Original Char is: {original_Char1}, Lowercase Char is: {lowercase_Char1}");
char original_Char2 = 'X';
char lowercase_Char2 = Char.ToLowerInvariant(original_Char2);
Console.WriteLine($"Original Char is: {original_Char2}, Lowercase Char is: {lowercase_Char2}");
char original_Char3 = '3';
char lowercase_Char3 = Char.ToLowerInvariant(original_Char3);
Console.WriteLine($"Original Char is: {original_Char3}, Lowercase Char is: {lowercase_Char3}");
string input_String = "HeLloWorlD";
string lowercase_String = ConvertToLowerCaseInvariant(input_String);
Console.WriteLine($"Original String is: {input_String}, Lowercase String is: {lowercase_String}");
}
static string ConvertToLowerCaseInvariant(string input)
{
char[] char_Array = input.ToCharArray();
for (int i = 0; i < char_Array.Length; i++)
{
char_Array[i] = Char.ToLowerInvariant(char_Array[i]);
}
return new string(char_Array);
}
}
Output:
Original Char is: S, Lowercase Char is: s
Original Char is: X, Lowercase Char is: x
Original Char is: 3, Lowercase Char is: 3
Original String is: HeLloWorlD, Lowercase String is: helloworld
Explanation:
- Character Conversion:
originalChar1, originalChar2, and original_Char3 are defined with the values 'S', 'X', and '3' respectively.
Following this, every individual character undergoes a transformation to lowercase through the utilization of Char.ToLowerInvariant method. The resulting lowercase characters are stored in lowercaseChar1, lowercaseChar2, and lowercase_Char3 variables.
- Demonstrating the Output:
"Console.WriteLine" is employed to showcase the outcomes of character conversion.
- Converting to Strings:
Applying "Char.ToLowerInvariant" to each character, the ConvertToLowerCaseInvariant function accepts a string (input) and converts it to lowercase. This function then provides the updated string as its output.
- Demonstrating String Outcomes:
"Console.WriteLine" showcases the initial and lowercase renditions of the provided string. The code illustrates the process of transforming characters to their lowercase forms using Char.ToLowerInvariant, implementing this conversion on single characters as well as complete strings.
Example 2:
Let's consider another instance to demonstrate the Char.ToLowerInvariant(Char) function in C#.
using System;
class Demo
{
public static void Main()
{
get('Q');
get('y');
get('F');
get('6');
}
public static void get(char c)
{
//Obtaining the Unicode character
// making use of the ToLowerInvariant() Method
char ch = Char.ToLowerInvariant(c);
// displaying the char value
Console.WriteLine("The lowercase equivalent value"+
" of the {0} is {1}", c, ch);
}
}
Output:
The lowercase equivalent value of the Q is q
The lowercase equivalent value of the y is y
The lowercase equivalent value of the F is f
The lowercase equivalent value of the 6 is 6
Explanation:
- Main Method:
The program's entry point is the Main method.
By providing an alternative symbol as a parameter, it triggers four invocations of the get function.
- Retrieve method:
The get function necessitates a character parameter named 'c'.
The lowercase version of the input character 'c' is retrieved within the function by utilizing the Char.ToLowerInvariant method.
The lowercase form of the provided character 'c' is retrieved within the function by employing the Char.ToLowerInvariant method. This specific method relies on the casing conventions of the invariant culture to transform a specified Unicode character to its lowercase counterpart.
The lowercase letter is saved in the variable 'ch'.
After that, a statement is printed to the console utilizing "Console.WriteLine", displaying the initial character 'c' alongside its corresponding lowercase 'ch'.
'Q', 'y', 'F', and '6' are the four unique characters that are utilized when the Main function calls the get method four times.
The software exhibits a notification with details for every invocation, showcasing the initial character and its corresponding lowercase representation.
The program demonstrates the process of converting characters to lowercase using the Char.ToLowerInvariant method, and subsequently displays the output for each character processed by the get method.