RefactoringEssentials
RefactoringEssentials copied to clipboard
"Add null check" breaks execution flow
The "Add null check" C# refactoring results in an unwanted change of execution flow. In this example
bool b = true;
object o = null;
if (b)
{
int i = (int)o;
}
else
{
/* else clause */
}
it changes code to
bool b = true;
object o = null;
if (b && (o != null))
{
int i = (int)o;
}
else
{
/* else clause */
}
which in case o is null results in execution of the 'else' clause where it was not supposed to execute.