In C#, the ToUpper()
method is used to convert a string to uppercase. It returns a new string that is the uppercase version of the original string. The original string is not modified.
Here’s an example of how to use the ToUpper()
method in C#:
string originalString = "Hello, World!"; string upperCaseString = originalString.ToUpper(); Console.WriteLine(upperCaseString); // Output: HELLO, WORLD!
In the example above, the ToUpper()
method is called on the originalString
, and the resulting uppercase string is stored in the upperCaseString
variable. The Console.WriteLine()
method is then used to print the uppercase string to the console.
Note that the ToUpper()
method uses the rules of the current culture by default. If you want to perform the conversion using the uppercase rules of a specific culture, you can pass the desired CultureInfo
object as an argument to the ToUpper()
method. For example:
string originalString = "münchen"; string upperCaseString = originalString.ToUpper(new CultureInfo("de-DE")); Console.WriteLine(upperCaseString); // Output: MÜNCHEN
In this case, the ToUpper()
method converts the string “münchen” to uppercase using the rules of the German (Germany) culture (“de-DE”). The resulting uppercase string is “MÜNCHEN”.
Signature:
The signature of the ToUpper()
method in C# is as follows:
public string ToUpper();
The ToUpper()
method is a member of the System.String
class and does not take any parameters. It returns a new string that is the uppercase version of the original string on which it is called.
Here’s the breakdown of the signature:
- Access Modifier:
public
- The
ToUpper()
method is accessible from any code in the same assembly or referencing assembly.
- The
- Return Type:
string
- The
ToUpper()
method returns a new string that represents the uppercase version of the original string.
- The
- Method Name:
ToUpper
- The name of the method is
ToUpper
.
- The name of the method is
- Parameters: None
- The
ToUpper()
method does not take any parameters.
- The
Note that the ToUpper()
method is an instance method, which means it is called on an instance of the string
class.
Parameter:
The ToUpper()
method does not accept any parameters. It is a parameterless method that operates on the instance of the string
class on which it is called.
Here is the corrected signature for the ToUpper()
method in C#:
public string ToUpper();
As mentioned earlier, it returns a new string that represents the uppercase version of the original string.
Return:
The ToUpper()
method in C# returns a new string that represents the uppercase version of the original string on which it is called. It does not modify the original string in-place but instead creates and returns a new string.
For example:
string originalString = "Hello, World!"; string upperCaseString = originalString.ToUpper(); Console.WriteLine(upperCaseString); // Output: HELLO, WORLD!
In the above code, the ToUpper()
method is called on the originalString
, and the resulting uppercase string is assigned to the upperCaseString
variable. The value of upperCaseString
will be “HELLO, WORLD!”, which is the uppercase version of the original string.
It’s important to note that the ToUpper()
method uses the rules of the current culture by default. If you need to perform the conversion using the uppercase rules of a specific culture, you can use the overloaded version of the ToUpper()
method that accepts a CultureInfo
parameter.
C# String ToUpper() Method Example:
Certainly! Here’s an example that demonstrates the usage of the ToUpper()
method in C#:
using System; class Program { static void Main() { string originalString = "Hello, World!"; string upperCaseString = originalString.ToUpper(); Console.WriteLine("Original String: " + originalString); Console.WriteLine("Uppercase String: " + upperCaseString); } }
Output:
Original String: Hello, World! Uppercase String: HELLO, WORLD!
In the above example, the ToUpper()
method is called on the originalString
, which converts it to uppercase. The resulting uppercase string is then assigned to the upperCaseString
variable. The original string remains unchanged.
Finally, the original string and the uppercase string are printed to the console using Console.WriteLine()
.