Рассматривая этот вопрос, который Джон отлично справился с ответом ... 'How to read a text file reversly with iterator'. И был аналогичный вопрос, в котором я ответил с помощью указателей hocus pocus .. '.net is there a way to read a text file from bottom to top' до того, как он закрылся.Почему вы не можете использовать небезопасное ключевое слово в контексте итератора?
Теперь я решил попытаться решить эту проблему с помощью указателей, по краям ...
public class ReadChar : IEnumerable<char> { private Stream _strm = null; private string _str = string.Empty; public ReadChar(string s) { this._str = s; } public ReadChar(Stream strm) { this._strm = strm; } public IEnumerator<char> GetEnumerator() { if (this._strm != null && this._strm.CanRead && this._strm.CanSeek) { return ReverseReadStream(); } if (this._str.Length > 0) { return ReverseRead(); } return null; } private IEnumerator<char> ReverseReadStream() { long lIndex = this._strm.Length; while (lIndex != 0 && this._strm.Position != 0) { this._strm.Seek(lIndex--, SeekOrigin.End); int nByte = this._strm.ReadByte(); yield return (char)nByte; } } private IEnumerator<char> ReverseRead() { unsafe { fixed (char* beg = this._str) { char* p = beg + this._str.Length; while (p-- != beg) { yield return *p; } } } } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } }
, но обнаружил, что C# компилятор не может справиться с этим, используя эту реализацию, но был опустошен, когда компилятор с # отказался с CS1629 ошибки - «небезопасный код не может появиться в итераторы»
Почему это так?
Подумайте о том, как 'yield return' работает под капотом. –