I am attempting to create a ternary expression and I am getting the following error
"Type of conditional expression cannot be determined because there is no implicit conversion between LiveSubscription and DisconnectedSubscription"
The same logic works in an if statement, but I wanted to understand why it won't work in a ternary expression -
Here is the gist of what I am trying to do:
public interface IClientSubscription
{
bool TryDisconnect();
}
public class LiveSubscription : IClientSubscription
{
public bool TryDisconnect()
{
return true;
}
}
public class DisconnectedSubscription : IClientSubscription
{
public bool TryDisconnect()
{
return true;
}
}
public class ConnectionManager
{
public readonly IClientSubscription Subscription;
public ConnectionManager(bool IsLive)
{
// This throws the exception
Subscription = (IsLive)
? new LiveSubscription()
: new DisconnectedSubscription();
// This works
if (IsLive)
{
Subscription = new LiveSubscription();
}
else
{
Subscription = new DisconnectedSubscription();
}
}
}
I could always switch it to an if/else but I wanted to understand what is going wrong first!
You need to cast at least one of the operands to
IClientSubscription
:The reason is that the ternary expression is of a certain type which is determined by the operands. Basically, it tries to cast the second operand to the type of the first or vice versa. Both fail here, because
LiveSubscription
isn't anDisconnectedSubscription
and vice versa.The compiler doesn't check whether both share a common base type.
Trying to answer your question in the comment:
No, ternary expressions are not some sort of object, but a ternary expression is the right hand part of an assignment. Each right hand part expression of an assignment has a certain type, otherwise it would be impossible to assign this expression to the variable on the left hand side.
Examples:
var x = Guid.NewGuid()
The right hand side expression (
Guid.NewGuid()
) is of typeGuid
, because the methodNewGuid()
returns aGuid
.var x = y.SomeMethod()
The right hand side expression is of the type of the return type of
SomeMethod()
.var x = IsLive ? "a" : 1
This is obviously invalid, isn't it? What type should
x
be? Astring
or anint
?This would lead to the exact same error message that you had with your code.
Your example a bit changed:
Note the
var
beforesubscription
, we now initialize a new variable, not an existing. I think even here, it is obvious what the problem is: What type shouldsubscription
be?LiveSubscription
orDisconnectedSubscription
? It can be neither, because depending onIsLive
it needs to be either the one or the other.About the comparison with
if
:In your code where you assign a new
LiveSubscription
instance or a newDisconnectedSubscription
instance toSubscription
an implicit cast toIClientSubscription
is occurring, because the compiler knows thatSubscription
is of typeIClientSubscription
and bothLiveSubscription
andDisconnectedSubscription
can implicitly be converted to that interface.The assignment with the ternary expression is a bit different, because the compiler first tries to evaluate the ternary expression and only afterwards it tries to assign it to
Subscription
. This means that the compiler doesn't know that the result of the ternary expression needs to be of typeIClientSubscription
.