There is a subtle bug in convert if
statement to ternary expression.
Consider the code below.
AfterRefactor is method after if statement has been refactored to ternary expression. It looks fine, but it has a subtle different behavior - the return type is long instead of an int.
Value 1 should be cast to object to make it correct, like
return flag ? (object)1 : (long)0;
C#Console.WriteLine(Tubo.Properly(true).GetType().Name);
Console.WriteLine(Tubo.AfterRefactor(true).GetType().Name);
internal class Tubo
{
public static object Properly(bool flag)
{
if (flag)
{
return (int)1;
}
else
{
return (long)0;
}
}
public static object AfterRefactor(bool flag)
{
return flag ? (int)1 : (long)0;
}
}
Miha,
I reproduced this behavior. We will research it and update this thread as soon as we have news.