V3202. Unreachable code detected. The 'case' value is out of the range of the match expression.
The analyzer has detected a possible error: one or several branches of the 'switch' statement are never executed. It occurs because the expression being compared cannot accept a value written after 'case'.
Take a look at a synthetic example:
switch (random.Next(0, 3))
{
case 0:
case 1:
Console.WriteLine("1");
break;
case 2:
Console.WriteLine("2");
break;
case 3: // <=
Console.WriteLine("3");
break;
default:
break;
}
In this example, the code in 'case 3' will never be executed. The reason is that in 'random.Next(0, 3)', the upper boundary is not included in the range of return values. As a result, the 'switch' expression will never take 3, and 'case 3' will not be executed.
We can fix it in two ways. As a first option, we can simply remove the dead code by deleting the 'case 3' section that is out of the 'random.Next(0, 3)' range:
switch (random.Next(0, 3))
{
case 0:
case 1:
Console.WriteLine("1");
break;
case 2:
Console.WriteLine("2");
break;
}
As a second, we can increase the upper bound in the 'next' method — 'random.Next(0, 4)':
switch (random.Next(0, 4))
{
case 0:
case 1:
Console.WriteLine("1");
break;
case 2:
Console.WriteLine("2");
break;
case 3:
Console.WriteLine("3");
break;
}
This diagnostic is classified as: