2

I dont understand why is this printing char data type once as char, other time as integer

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

I would expect output to be A, A, A but the output of above is A, 65, 65. Why?

pixel
  • 9,653
  • 16
  • 82
  • 149
  • 3
    Did you try looking at the documentation? It would be faster . – Crowcoder Jan 20 '18 at 00:16
  • 4
    The expression `(condition) ? x : y` can only have one type, determined at compile time. Since you can cast `char` to `int` but not vice versa, that type is `int`. Therefore, the `WriteLine(int)` override is called. – Blorgbeard Jan 20 '18 at 00:18
  • 1
    If you look at the documentation [here](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-operator), it says, *"Either the type of first_expression and second_expression must be the same, or an implicit conversion must exist from one type to the other."* Since there is an implicit conversion from `char` to `int`, but not the other way around, the type becomes an `int` (implicit conversion means it works without an explicit cast - you can do `int foo = 'A';`, but you can't do `char foo = 1;`). – Rufus L Jan 20 '18 at 00:27

1 Answers1

3

The ternary/conditional operator ? requires all of the following three operands:

  1. An expression that evaluates to a boolean
  2. An expression that returns a value of any type
  3. An expression that returns a value of the same type as #2

The return value will always be of the same type; that is why #2 and #3 must be the same type.

If the third operand isn't the same type as the second operand, the compiler will look for an implicit cast and use it if possible.

So when you write

var x = flag ? 65 : 'A';

it is exactly the same as

int x = flag ? (int)65 : (int)'A';

...and will always return an int.

If this were not the case, the result of the ? could not be assigned to a strongly typed variable, which would be a serious obstacle.

Also, you cannot write something like this:

var x = flag ? 65 : "A"; //Notice it's a string and not a char

...because there is no implicit cast from "A" to integer.

John Wu
  • 50,556
  • 8
  • 44
  • 80
  • Potentially of interest to OP, if the #2 is casted to an `object` the initial example will give their expected output, but only because `Console.WriteLine` only really cares about `ToString`, the object types returned by the expression will now both be `object` and not different types like `char`/`int`. – McAden Jan 20 '18 at 00:25
  • [The operator is `?:`, not just `?`.](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-operator) – poke Jan 20 '18 at 01:15