I have this piece of code
StateMachine.State = string.IsNullOrEmpty(MyString) ?
(IState) StateMachine.StateA : StateMachine.StateB;
I can also write it as
if (string.IsNullOrEmpty(MyString))
StateMachine.State = StateMachine.StateA;
else
StateMachine.State = StateMachine.StateB;
State
is of type IState
and StateA
and StateB
both implement IState
.
In the first snippet the compiler demands an explicit cast, while in the second it is not needed. Why is it needed in the first example?
EDIT: The suggested duplicate question doesn't completely cover my question. My question is about objects and interfaces, while the other question is about primitive data types and constant numbers. Especially the suggestion of quetzalcoatl about the declarations is very valuable.
Reading the answer on the suggested duplicate question never would have pointed me in that direction.