【发布时间】:2013-09-23 22:28:32
【问题描述】:
考虑以下代码(LinqPad 中的 .Dump() 只是写入控制台):
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
}
鉴于上述结果,为什么 TextReader.Read() 返回 int 而不是 char。什么情况下会读取大于2字节的值?
【问题讨论】:
-
首先,
Read()是虚拟的,StringReader覆盖它。为什么返回int?不确定..但至少最后一次调用Read()将允许它确定EOF。此外,它还通过内部字符串索引器 ([]) 方法将char转换为int。
标签: c# unicode character-encoding textreader