C
C# Learner
In the following code, I get a compile error referring to the "return"
statement.
static object GetMultiColorElement(string s, bool isFade)
{
//...
return isFade ?
new ChatMessageFadeTagElement(colors) :
new ChatMessageAltTagElement(colors);
}
The compile error message is:
[begin]
Type of conditional expression can't be determined because there is no
implicit conversion between 'ChatMessageFadeTagElement' and
'ChatMessageAltTagElement'
[end]
'ChatMessageFadeTagElement' and 'ChatMessageAltTagElement' are both
direct subclasses of 'object'.
The following resolves the error:
static object GetMultiColorElement(string s, bool isFade)
{
//...
return isFade ?
new ChatMessageFadeTagElement(colors) :
new ChatMessageAltTagElement(colors)
as object;
}
Why do I need to cast here?
statement.
static object GetMultiColorElement(string s, bool isFade)
{
//...
return isFade ?
new ChatMessageFadeTagElement(colors) :
new ChatMessageAltTagElement(colors);
}
The compile error message is:
[begin]
Type of conditional expression can't be determined because there is no
implicit conversion between 'ChatMessageFadeTagElement' and
'ChatMessageAltTagElement'
[end]
'ChatMessageFadeTagElement' and 'ChatMessageAltTagElement' are both
direct subclasses of 'object'.
The following resolves the error:
static object GetMultiColorElement(string s, bool isFade)
{
//...
return isFade ?
new ChatMessageFadeTagElement(colors) :
new ChatMessageAltTagElement(colors)
as object;
}
Why do I need to cast here?