Why this matters
Variables that never change should be marked as `const` to prevent accidental modification and improve code clarity.
Ensure that variables that never change are marked as `const` to prevent accidental modification and enhance code clarity.
Variables that never change should be marked as `const` to prevent accidental modification and improve code clarity.
Side-by-side examples engineers can pattern-match during review.
public bool Seek(int[] input)
{
var target = 32; // Noncompliant
foreach (int i in input)
{
if (i == target)
{
return true;
}
}
return false;
}public bool Seek(int[] input)
{
const int target = 32;
foreach (int i in input)
{
if (i == target)
{
return true;
}
}
return false;
}public bool Seek(int[] input)
{
var target = 32; // Noncompliant
foreach (int i in input)
{
if (i == target)
{
return true;
}
}
return false;
}public bool Seek(int[] input)
{
const int target = 32;
foreach (int i in input)
{
if (i == target)
{
return true;
}
}
return false;
}From the same buckets as this rule.