Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Design reasons behind making ToUpper a static method on Char

Tags:

c#

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.

like image 850
Francois Beaussier Avatar asked Jul 24 '16 04:07

Francois Beaussier


People also ask

Does ToUpper work on char?

ToUpper(Char, CultureInfo) Converts the value of a specified Unicode character to its uppercase equivalent using specified culture-specific formatting information.

What is the purpose of static method in Java?

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.

Is toUpperCase a static method Java?

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.

Should I use static methods C#?

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.

What is the use of toupper () method in C?

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.

What is the use of touppercase method of character class?

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.

What is CHCH and touppercase in JavaScript?

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.

How to convert a character to uppercase using the current culture?

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.


2 Answers

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.

Edit - Why this question is actually speculation

As I'm very curious to see if there's an actual answer to "why do chars 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 chars 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).

like image 50
JoshuaTheMiller Avatar answered Oct 18 '22 18:10

JoshuaTheMiller


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()
like image 26
Wyck Avatar answered Oct 18 '22 18:10

Wyck