Jon이 대답 할 때 훌륭한 일을했던이 질문을 보면서 ... 'How to read a text file reversly with iterator'. 그리고 비슷한 질문이있었습니다. 포인터를 사용하여 답변을 얻었습니다. '.net is there a way to read a text file from bottom to top'이 닫히기 전에 ....반복기 컨텍스트에서 안전하지 않은 키워드를 사용할 수없는 이유는 무엇입니까?
포인터를 사용하여이 문제를 해결하려고했습니다. 좋아, 거칠고 거친 것처럼 보입니다. C# 컴파일러 오류 CS1629와 거부 할 때 가장자리 주위에 ...
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# 컴파일러는이 구현을 사용하여이 문제를 처리 할 수 있다는 것을 발견하지만 - '에 나타나지 않을 수 있습니다 안전하지 않은 코드 반복자'왜 이렇게이다?
'yield yield'가 어떻게 작동하는지 생각해보십시오. –