Home > DeveloperSection > Forums > char char = int? Why?
Royce Roy
Royce Roy

Total Post:146

Posted on    June-06-2013 2:41 AM

 C# C# 

 1 Reply(s)
 1089  View(s)
Rate this:
Hi All,

Why is adding two char in C# results to an int type?

For example, when I do this:

var pr = 'R' + 'G' + 'B' + 'Y' + 'P';
the pr variable becomes an int type. I expect it to be a string type with a value of "RGBYP".

Why is C# designed like this? Wasn't the default implementation of adding two chars should be resulting to a string that concatenates the chars, not int?

Thanks in advance. 


Total Post:604

Posted on    June-06-2013 8:24 AM


You can simply convert the first of your chars to a string, to outwit your compiler:

var pr = 'R'.ToString() + 'G' + 'B' + 'Y' + 'P';

You could also define a char array and then use the string constructor:

char[] letters = { 'R', 'G', 'B','Y', 'P' };

string alphabet = new string(letters);
If you want to print out a character solely, you always have to convert it to a string, to get its text representation:

 var foo1 = 'F';

Thanks in advance.

Don't want to miss updates? Please click the below button!

Follow MindStick