Why does C # not define an add operation for char?

As the name says. Regarding this question.

Why is the following code not working? This seems reasonable logically.

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

Now, from the above question, we can conclude what is happening here: the compiler uses the implicit conversion of char β†’ int, and then adds these values. This makes me think that there should not be an addition operation defined for char s! Is that true, and if so, why not?

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

+7
c #
source share
6 answers

First of all, a word about your deductive process. Your conclusion is that char is converted to int, and therefore there is no addition operator defined on char - that is the point, so good on you. But I note that the deduction process is not needed. You could just read section 7.7.4 of the specification, which clearly describes all the built-in addition operators. In short, it is int + int, uint + uint, long + long, ulong + ulong, float + float, double + double, decimal + decimal, enum-and-base type, delegate command, line + line, line + object and object + line. (And, of course, options with canceled to zero values ​​that include value types.) So, yes, there are no addition operators that accept characters.

Secondly, to answer your question "why not?". - The characters are a little strange. Their memory is short integers, but their semantics are the characters of characters in a string. So, should char be considered as an integer form or as a short string? In this case, the language developers decided to follow the example of earlier languages, such as C, and process characters, such as integers, when doing math on them that does not include strings. If you want to treat them as short strings, there are many ways to convert characters to strings. Adding an empty string, as another answer suggests, unambiguously tells the compiler "this operation is performed in strings, not in the value of an integer character".

+15
source share

What you are really looking for is:

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

This is what the compiler does with the + operator on the lines anyway.

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

+7
source share

I think you need to focus on the word β€œwhy” in your question. In C #, the behavior of adding two characters was defined as meaning "add Unicode values" rather than "concatenate". We agree that this could be more reasonably defined as meaning "concatenation", given that strings and characters are much clearer integer numbers than they were in past lives (for example, the C programming language).

'a' is a character constant. Traditionally, character constant, at least in C, was just another way of representing an integer. In this case, the Unicode value of the specified integer ('a' = 97, 'b' = 98, etc.). Therefore, the expression 'a' + 'b' is customary to mean that they combine these two Unicode values.

To achieve what you expect, do

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

As already mentioned, char is considered a form of the whole, and not a string, therefore, in principle, the addition operation is to add two numbers, and not to perform concatenation. You can enter two characters in a string as follows:

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

To add your characters, you can do one of several things similar to your original intentions:

 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 key by discarding an empty string and forcefully implicitly converting the char to a string conversion for each char.

For the foo3 example, you use a ToString call for each character to add two lines.

+1
source share

You can use the operator overload for the string and try the following:

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

but do not confuse with

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

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

The idea is to overload the + operator to enter a string, and for this you must have the first argument as a string.

This sounds like a hoax, but C # is pointless and merciless! (this is actually .Net, which does not have an operator + overload for (Char, Char) and C # tries to use Char for Int32).

0
source share

All Articles