I’m experiencing unexpected compiler errors with this code:
bool b = true; //or false
StringBuilder builder = ...; // a string builder filled with content
IVersePart vp = b ? (DualLanguageVersePart)builder : (VersePart)builder;
Both DualLanguageVersePart and VersePart implement the IVersePart interface.
Both DualLanguageVersePart and VersePart have an explicit cast operator form StringBuilder.
Since both classes implement the interface that’s the type of vp, I would expect this to work flawlessly, or at least compile properly. Instead the compiler reports that no implicit conversion can be done between the two types.
Why is this not working?
I’ve had this issue before, ternary operator requires both types of the true result or false result either be the same type, or you cast them to the same type.