title: Why you cannot use an unsafe keyword in an iterator context?
body: <p>In looking at this question which Jon did a fine job in answering... '<a href="https://stackoverflow.com/questions/452902/how-to-read-a-text-file-reversely-with-iterator-in-c">How to read a text file reversly with iterator</a>'. And there was a similar question in which I answered using pointers hocus pocus..'<a href="https://stackoverflow.com/questions/2241012/net-is-there-a-way-to-read-a-txt-file-from-bottom-to-top/2241173#2241173">.net is there a way to read a text file from bottom to top</a>' before it got closed....</p>
  
  <p>Now I did set out to try solve this using pointers, ok, it looks hackish and rough around the edges...</p>
  
  <pre>
  public class ReadChar : IEnumerable&lt;char&gt;
  {
      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&lt;char&gt; GetEnumerator()
      {
          if (this._strm != null && this._strm.CanRead && this._strm.CanSeek)
          {
              return ReverseReadStream();
          }
          if (this._str.Length &gt; 0)
          {
              return ReverseRead();
          }
          return null;
      }
  
      private IEnumerator&lt;char&gt; 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&lt;char&gt; ReverseRead()
      {
          unsafe
          {
              fixed (char* beg = this._str)
              {
                  char* p = beg + this._str.Length;
                  while (p-- != beg)
                  {
                      yield return *p;
                  }
              }
          }
      }
      IEnumerator IEnumerable.GetEnumerator()
      {
          return GetEnumerator();
      }
  }
  </pre>
  
  <p>but discovered that C# compiler cannot handle this using this implementation but was devastated when the C# compiler refused with an error CS1629 - 'Unsafe code may not appear in iterators'</p>
  
  <p>Why is that so? </p>