CHAR CHAR = INT? WHY?

Royce Roy

Total Post:149

Points:1043
Posted by  Royce Roy
C# 
C#
 1267  View(s)
Ratings:
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. AVADHESH PATEL

    Post:604

    Points:4228
    Re: char char = int? Why?

    Hi,


    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';
     MessageBox.Show(foo1.ToString());

    Thanks in advance.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!