Total .NET Analyzer Rule Documentation  

EmptyCase Rule

Review empty Case statements.

Remarks

Select Case statements (in Visual Basic) and Switch statements (in C#) evaluate a given expression, and performed a different action based on the result. By having no code in a Case statement, you are not firing an action the expression evaluates to a specific result.

This programming technique is valid in some cases, such as when you want to "ignore" a certain expression result. For instance, the following code ignores the value of 0:


' VB
Select Case level
Case 1
' no commission
Case 2
commission = totalSales * 0.03
Case 3
commission = totalSales * 0.05
Case Else
commission = totalSales * 0.1
End Select

//C#
switch(level)
{
case 1:
// no commission
break;
case 2:
commission = totalSales * 0.03;
break;
case 3:
commission = totalSales * 0.05;
break;
default:
commission = totalSales * 0.10;
break;
}


You should, however, review any empty Case statements in your code to determine whether an action is needed for the expression result. Empty Case statements may represent unfinished work or commented out code that you actually need.

Resolution

If you have an empty Case statement, you are effectively ignoring the expression result. You should examine all empty Case statements to determine whether this is in fact appropriate, or whether some action is needed.

See Also

Select...Case Statements (VB)

Switch Statements (C#)