Wednesday, 18 September 2013

Why does TextReader.Read return an int, not a char?

Why does TextReader.Read return an int, not a char?

Consider the following code ( .Dump() in LinqPad simply writes to the
console):
var s = "��"; //3 byte code point. 4 byte UTF32 encoded
s.Dump();
s.Length.Dump(); // 2
TextReader sr = new StringReader("��");
int i;
while((i = sr.Read()) >= 0)
{
// notice here we are yielded two
// 2 byte values, but as ints
i.ToString("X").Dump(); // D852, DF62
}
Given the outcome above, why does TextReader.Read() return an int and not
a char. Under what circumstances might it read a value greater than 2
bytes?

No comments:

Post a Comment