VB.NET
Chr(i) and C#
(char)i are not equivalent.
To obtain the same behavior, through VB.NET you have to call
ChrW function instead of
Chr function.
The first one (ChrW) accepts UNICODE values (between -32768 and 65535), whereas the second (Chr) accepts ASCII values (between 0 and 255).
Internally, ChrW always calls
Convert.ToChar, the same as the explicit cast in C#.
Chr calls
Convert.ToChar only when the value is between 0 and 127. For greater values it uses the
Encoding object for ANSI code pages.
It is explained at
Chr, ChrW Functions[
^].
If you instead want to obtain in C# the same behavior from VB.NET, then you can:
1) In your C# project, add an assembly reference to Microsoft.VisualBasic, then replace
(char)i
with
Microsoft.VisualBasic.Strings.Chr(i)
.
2) You can emulate Microsoft.VisualBasic.Strings.Chr by replacing the cast with a call to Encoding.GetChars, as follows:
var culture = System.Threading.Thread.CurrentThread.CurrentCulture;
var encoding = Encoding.GetEncoding(culture.TextInfo.ANSICodePage);
char[] buffer = new char[1];
for (int i = 0; i <= 255; i++)
{
encoding.GetChars(new byte[] { (byte)i }, 0, 1, buffer, 0);
s[i] = i.ToString() + "=" + buffer[0].ToString();
}
Note that
Encoding is defined inside System.Text namespace.
Regards,
Daniele.