"for" loop appears to behaves abnormally when put in test to iterate through a whole range of "Integers", "Double", "byte", etc. However, this seems to be buggy but this situation arises due to "check arithmetic" that is used by default when incrementing the loop control variable beyond its maximum value which makes it value overflows and the loop restart's at the type's minimum value.
At first glance following example of for loop iteration seems to be fine and appears that it will iterate through the full range of the byte data type. However, the loop control variable can never exceeds than byte.MaxValue and it will reinitializes to byte.MinValue which makes it an infinite loop as predicate always evaluate it to true.
for (byte b = byte.MinValue; b <= byte.MaxValue; b++)
{
Console.WriteLine(b);
}
At first glance following example of for loop iteration seems to be fine and appears that it will iterate through the full range of the byte data type. However, the loop control variable can never exceeds than byte.MaxValue and it will reinitializes to byte.MinValue which makes it an infinite loop as predicate always evaluate it to true.
for (byte b = byte.MinValue; b <= byte.MaxValue; b++)
{
Console.WriteLine(b);
}