The problem is tha code
overflows and returns to 0
after being Char.MaxValue
. The for
cycle then doesn't end.
Try
var theWholeUTF8 = new StringBuilder();
for (int code = Char.MinValue; code <= Char.MaxValue; code++)
{
theWholeUTF8.Append((char)code);
}
To make it clear... At a certain point
code = Char.MaxValue - 1
code++; // code == Char.MaxValue
is code <= Char.MaxValue? Yes
theWholeUTF8.Append((char)code);
code++; // code == 0
is code <= Char.MaxValue? Yes
theWholeUTF8.Append((char)code);
and so on!
One possible solution is to use for the code
a bigger variable. Another solution is:
for (char code = Char.MinValue; code < Char.MaxValue; code++)
{
theWholeUTF8.Append(code);
}
theWholeUTF8.Append(Char.MaxValue);
where we stop when code == Char.MaxValue
and we add manually the Char.MaxValue
.
Other solution, obtained by moving the check BEFORE the addition:
char code = Char.MinValue;
while (true)
{
theWholeUTF8.Append(code);
if (code == Char.MaxValue)
{
break;
}
code++;
}