简体   繁体   中英

Why does C# not define an addition operation for char's?

As the title says. Related to this question.

Why does the following code not work? It seems reasonable logically.

string foo = 'a' + 'b'; // Fails with an invalid int -> string conversion

Now, from the above linked question we can infer what is going on here: the compiler is using the implicit char -> int conversion, then adding those values. This leads me to believe that there mustn't be an addition operation defined for char s! Is this true, and if so, why is there none?

EDIT: The general consensus is that it isn't so much that there isn't one defined so much as the one that I expect to be defined isn't.

First off, a word about your deductive process. Your deduction -- that char is converted to int, and therefore there is no addition operator defined on char -- is spot on, so good on you. But I note that the deduction process was unnecessary. You could have simply read section 7.7.4 of the specification, which clearly describes all the built-in addition operators. Briefly, they are int + int, uint + uint, long + long, ulong + ulong, float + float, double + double, decimal + decimal, enum-and-underlying type, delegate combination, string + string, string + object and object + string. (And of course, the lifted-to-nullable versions of those that involve value types.) So yes, there are no addition operators which take chars.

Second, to answer your "why not?" -- chars are a bit weird. Their storage is that of short integers, but their semantics are those of the characters in a string. So should a char be treated as its integer form, or as a short string? The language designers decided in this case to follow the lead of earlier languages like C and treat chars like integers when doing mathematics on them that does not involve strings. If you want to treat them like short strings, there are any number of ways to convert chars to strings. Appending the empty string, as another answer suggests, unambiguously tells the compiler "this operation is on strings, not on the character's integer value".

What you are really looking for is this:

string foo = String.Concat('a', 'b');

Which is what the compiler does with the '+' operator on strings anyways.

This is about twice as fast as any String.Format operation.

I'm thinking one should focus on the word "why" in your question. In C#, the behavior of adding two characters has been defined to mean "add the Unicode values" rather than "concatenate". We are in agreement this this could have been more reasonably defined to mean "concatenate" given that strings and chars are much more distinct entities from integers than they were in past lives (eg, The C Programming Language).

'a' is a character constant. Traditionally, a character constant, at least in C, was just another way to represent an integer. In this case, the Unicode value of said integer ('a' = 97, 'b' = 98, etc.). Hence, the expression 'a' + 'b' is taken to mean add those two Unicode values together.

To achieve what you're expecting, do

String foo = "a" + "b";  // double quotes = single-character strings to concatenate

As has already been said, char is considered a form of integer rather than a string, so fundamentally an addition operation is adding two numbers, rather than performing concatenation. You can get two characters into a string like this:

string str = String.Format("{0}{1}", 'a', 'b');

To get your chars to "add" up you could do one of several things similar to your original intent:

string foo2 = 'a' + "" + 'b'; //  Succeeds
string foo3 = 'a'.ToString() + 'b'.ToString(); // Succeeds
string foo4 = string.Concat('a', 'b'); // Succeeds

For the foo2 example you give the compiler a clue by dropping in an empty string and force an implicit char to string conversion for each char individually.

For the foo3 example you use the ToString call on each character to allow the addition of two strings.

You could use overload of addition operator for string and try this:

var result = "" + 'a' + 'b';

but please don't confuse with

var result = 'a' + 'b' + "";  // BAD !!!!!

as it will give you int converted to string and equal to "195".

The idea is to have overload of operator + for string to kick in and you should have first argument as string to achieve this.

It looks a bit like cheating but that's C# - senseless and merciless! (actually it's .Net which doesn't have operator + overload for (Char, Char) and C# trying to cast Char to Int32).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM