I have a simple loop which should look like this
for (sbyte i = sbyte.MinValue; i <= sbyte.MaxValue; i++)
{
Console.WriteLine(i);
}
unfortunately sbyte.MaxValue +1 = sbyte.MinValue
so this never meets the ending condition. My workaround was using int
from -128
to 127
but is there also a native sbyte
approach?
Not taking into consideration an obvious
output all except
MaxValue
, then outputMaxValue
approach, I see one solution.
It works, but looks weird and throws OverflowException
if checked
:)
sbyte i = sbyte.MinValue;
do
{
Console.WriteLine(i++);
} while (i != sbyte.MinValue);
You can try this :
for (sbyte i = sbyte.MinValue; i <= sbyte.MaxValue; i++)
{
Console.WriteLine(i);
if(i==sbyte.MaxValue)
break;
}
You can use an int
just for the condition and the sbyte
inside.
int checkVal = sbyte.MinValue;
for (sbyte i = sbyte.MinValue; checkVal <= sbyte.MaxValue; i++, checkVal++)
{
Console.WriteLine(i);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With