4

I am surprised by the output of the following code

public static void Main(string[] args)
{
    char x = 'A';
    int i = 0;
    Console.WriteLine (true  ? x : 0);
    Console.WriteLine(false ? i : x); 
}

When I was reading the C# interview question I saw this code and the output of above code was

Output

65
65

I am wondering how does that happen.

Can anybody explain me ? Thanks!

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
Chris Hadfield
  • 494
  • 1
  • 7
  • 34
  • 2
    Nitpick: It has nothing to do with ASCII (even though it does). The Unicode code point for capital A is \u0041. The decimal equivalent of the hex 0x41 is 65 decimal. The `char` type is an integral type, and the conditional ternary operator needs compatible types so it considers `'A'` as an integer, rather than a "character". If you had two characters (something like this: `Console.WriteLine(true ? 'A' : 'B');`), you would have seen `'A'` as the output – Flydog57 Nov 02 '18 at 05:24
  • 1
    BTW, if you had mixed the string `"A"` with an integer, it wouldn't have compiled, you would have gotten this error: `Type of conditional expression cannot be determined because there is no implicit conversion between 'string' and 'int'`. It's the fact that characters are implicitly convertible to `int`s that lots of things _Just Work_. One more historical fact... The "C Language" could do this too, and C# works hard to make sense to us really old C programmers (those of us who bought the first edition of K&R's "The C Programming Language"), – Flydog57 Nov 02 '18 at 05:36
  • 1
    http://www.asciitable.com/ – Ňɏssa Pøngjǣrdenlarp Nov 02 '18 at 05:36
  • Possible duplicate of [C# Console.WriteLine prints a Char type as Integer](https://stackoverflow.com/questions/48351313/c-sharp-console-writeline-prints-a-char-type-as-integer) – Lance U. Matthews Nov 02 '18 at 06:09
  • 1
    @John - done sir – Chris Hadfield Nov 04 '18 at 07:01

1 Answers1

7

'A' is a char and has the value of 65

However, there is a technical explanation for why this results in an integer (and not the string representation of a char), you can find it in the ECMA C# specifications

12.15 Conditional operator

The second and third operands, x and y, of the ?: operator control the type of the conditional expression.

  • If x has type X and y has type Y then,
    • If X and Y are the same type, then this is the type of the conditional expression.
    • Otherwise, if an implicit conversion (§11.2) exists from X to Y, but not from Y to X, then Y is the type of the conditional expression.
    • Otherwise, if an implicit enumeration conversion (§11.2.4) exists from X to Y, then Y is the type of the conditional expression.
    • Otherwise, if an implicit enumeration conversion (§11.2.4) exists from Y to X, then X is the type of the conditional expression.
    • Otherwise, if an implicit conversion (§11.2) exists from Y to X, but not from X to Y, then X is the type of the conditional expression.
    • Otherwise, no expression type can be determined, and a compile-time error occurs.
  • If only one of x and y has a type, and both x and y are implicitly convertible to that type, then that is the type of the conditional expression.
  • Otherwise, no expression type can be determined, and a compile-time error occurs

Example

char Y = 'A';
int X = Y;
Y = X; // compiler error

In short, there is no implicit conversion from int to char, yet there is from char to int, so it makes sense for the result type to be an int

TheGeneral
  • 79,002
  • 9
  • 103
  • 141