If i try to do this:
// As an example. char? c value is actually given by the user.
char? c = null;
WriteLine((c == null) ? "null" : c);
Where i am saying that if c equals null (c == null)
then WriteLine()
should output null to the console. Else it should output the value of c.
However i get this compiler error:
Type of conditional expression cannot be determined because there is no implicit conversion between 'string' and char?.
My workaround was to do this:
char? c = null;
WriteLine((c == null) ? "null" : "{0}",c);
Or with C#6 and using String Interpolation
char? c = null;
WriteLine((c == null) ? "null" : $"{c}");
Reading through similar compiler errors here in stackoverflow e.g.
or
Tells that the reason is the types must match. My question is why must the types match? Also since the error says there is no implicit conversion between string and char?, does that mean that there is an explicit conversion of string to char? (which obviously by logic can't be tha case here) or does it mean that there is from char? to string ?