In C#, we have this non-static method on the type string:
"abc".ToUpper()
but for char, we need to use a static method:
char.ToUpper('a')
When introducing c# to beginners, they always expect to be able to write the following:
'a'.ToUpper()
Does anyone have insights as why it was designed like this?
The only thing I can think of is performance but then I would have expected a static ToUpper() for the type string too.
ToUpper(Char, CultureInfo) Converts the value of a specified Unicode character to its uppercase equivalent using specified culture-specific formatting information.
The static keyword is used to create methods that will exist independently of any instances created for the class. Static methods do not use any instance variables of any object of the class they are defined in.
toUpperCase(), so in Java they had from the beginning a static method style for primitives ( Character. toUpperCase('a') ) and a member method for String, which is an Object ( myStr.
Static methods are usually useful for operations that don't require any data from an instance of the class (from this ) and can perform their intended purpose solely using their arguments.
In C#, ToUpper () is a string method. It converts every characters to uppercase (if there an an uppercase version). If a character does not have an uppercase equivalent, it remains unchanged. For example, special symbols remain unchanged. This method can be overloaded by passing different type of arguments to it.
The toUpperCase(char ch) method of Character class converts the given character argument to the uppercase using a case mapping information which is provided by the Unicode Data file.
ch: It is the character that needs to be converted. The toUpperCase (char ch)method returns the uppercase of the given character. Otherwise, this method returns the character itself.
To convert a character to uppercase by using the casing conventions of the current culture, call the ToUpper (Char, CultureInfo) method overload with a value of CurrentCulture for its culture parameter.
The difference lies in the fact that string
is a reference type, and char
is a keyword that represents the .Net Framework's Char Structure. When you call Char.ToUpper('a')
you are actually using the Char Structure in C#. Structures are Value Types. Value Types are immutable.
Since structs are immutable, methods that act upon the struct itself do not work as expected (see Why are Mutable Structs Evil). Thus static methods are needed. When calling Char.ToUpper(aChar)
you are not actually changing aChar, instead, you are creating a new instance of a character that is the uppercase representation of the character you passed in as a parameter and returning it. The example below demonstrates this.
Char aChar = 'a';
Char.ToUpper(aChar);
//aChar still equals 'a'
Char bChar = 'b';
bChar = Char.ToUpper(bChar);
//bChar now equals 'B'
The reason char has other methods that allow you to do things like 'a'.Equals('a');
is because value types and reference types both inherit from Object, which defines those methods (technically, value types are of type System.ValueType
, which inherits from System.Object
). These methods do not enact any changes to the object itself.
As I'm very curious to see if there's an actual answer to "why do char
s not have a .ToUpper()
method", I decided to check out the CSharp 5 Language Specification Document, I have found the following:
char
is an Integral Type (pg 80), which is a subset of Simple Types. Simple Types themselves are just predefined Struct Types. Struct types are Value Types that "can declare constants, fields, methods, properties, indexers, operators, instance constructors, static constructors, and nested types" (pg 79).
string
is a Class Type, which is a Reference Type (pg 85). Class Types define "a data structure that contains data members (constants and fields), function members (methods, properties, events, indexers, operators, instance constructors, destructors and static constructors), and nested types" (pg 84).
At this point, it is obvious that char
s can support a .ToUpper()
method (which is why the extension method works). However, as the question states, they do not support one. At this point I'm convinced any reasoning as to why this is true is pure speculation (unless you're on the C# team, of course).
Hans Passant mentioned that it is possible to achieve this syntax easily via extension methods. I'll provide the code here in case anyone is deeply attached to using that syntax.
public static class MyExtensionMethods
{
public static char ToUpper( this char c )
{
return char.ToUpper( c );
}
}
Then you can do:
'a'.ToUpper()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With