char char = int? Why?

Total Post:149


 1740  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. 

  1. Re: char char = int? Why?


    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.


Please check, If you want to make this post sponsored

You are not a Sponsored Member. Click Here to Subscribe the Membership.