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

后端 未结 6 947
感动是毒
感动是毒 2020-12-11 04:07

As the title says. Related to this question.

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

string foo = \'a\' + \'b\'; // Fail         


        
6条回答
  •  Happy的楠姐
    2020-12-11 04:33

    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');
    

提交回复
热议问题