public void Main()
{
int i = 0; // Non compliant, i is never reassigned and can be made constant
Console.WriteLine(i);
}
Variable can be made constant.
Unlike variables, constant values are known at compile time and are injected as is in the code, requiring no runtime processing and therefore reducing the environmental footprint. Although good compilers will const eligible variables by themselves, it is still good practice to declare them constant, as it makes the code intent clearer.
This rule should not be ignored.
public void Main()
{
int i = 0; // Non compliant, i is never reassigned and can be made constant
Console.WriteLine(i);
}
public void Main()
{
const int i = 0; // Compliant, i is declared as constant
Console.WriteLine(i);
}
public void Main()
{
int i = 0; // Compliant, i is reassigned in the next line
Console.WriteLine(i++);
}