CS.STMT.IFELSE.BLOCK

Body for If/Else statement should be a block.

The body of an if / else statement must be a block between {and}.

Vulnerability and risk

If you omit the curly braces if in the body of the if / else statement, a careless programming error may occur.

Mitigation and prevention

Use curly braces in the body of an if / else statement.

Vulnerable code example

Copy
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  
  namespace kmcustom
  {
      class C18
     {
         public void doSomething()
         {
 
         }
 
         public void testNGIf(int some_number)
         {
 
             if (some_number == 1)
                 doSomething();
             else if (some_number == 2)
                 doSomething();
             else
                 doSomething();
 
 
             if (some_number == 3) ;
             else if (some_number == 4) ;
             else;
 
         }
 
         public void testOKIf(int some_number)
         {
 
             if (some_number == 1)
             {
                 doSomething();
             }
             else if (some_number == 2)
             {
                 doSomething();
             }
             else {
                 doSomething();
             }
         }
     }
 }