char
is a value type, meaning it has a numerical value (its UTF-16 Unicode ordinal). However, it is not considered a numeric type (like int, float, etc) and therefore, the + operator is not defined for char.
The char
type can, however, be implicitly converted to the numeric int
type. Because it's implicit, the compiler is allowed to make the conversion for you, according to a set of rules of precedence laid out in the C# spec. int
is one of the first things normally tried. That makes the +
operator valid, and so that's the operation performed.
To do what you want, start with an empty string:
var pr = "" + 'R' + 'G' + 'B' + 'Y' + 'P';
Unlike the char type, the string type defines an overloaded + operator for Object, which transforms the second term, whatever it is, into a string using ToString()
before concatenating it to the first term. That means no implicit casting is performed; your pr
variable is now inferred as a string and is the concatenation of all character values.