[英]Why you cannot use an unsafe keyword in an iterator context?
在查看 Jon 在回答這個問題時做得很好的問題時......“ 如何使用迭代器反向讀取文本文件”。 還有一個類似的問題,我用指針 hocus pocus 回答了這個問題......'. net 有沒有辦法在它關閉之前從下到上讀取文本文件......
現在我確實開始嘗試使用指針來解決這個問題,好吧,它的邊緣看起來很粗糙和粗糙......
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# 編譯器無法使用此實現處理此問題,但當 C# 編譯器拒絕並出現錯誤 CS1629 - '不安全的代碼可能不會出現在迭代器中'時,它被摧毀了
為什么呢?
Eric Lippert 有一篇關於這個主題的優秀博客文章: 迭代器塊,第六部分:為什么沒有不安全的代碼?
我想知道的是為什么你會為此使用指針。 為什么不簡單地說:
private IEnumerator<char> ReverseRead()
{
int len = _str.Length;
for(int i = 0; i < len; ++i)
yield return _str[len - i - 1];
}
亂用指針有什么令人信服的好處?
它只是 C# 規范的一部分:
26.1 迭代器塊……迭代器塊包含不安全的上下文是編譯時錯誤(第 27.1 節)。 迭代器塊總是定義一個安全的上下文,即使它的聲明嵌套在一個不安全的上下文中。
據推測,由編譯器生成的代碼需要是可驗證的,以免被標記為“不安全”。 如果要使用指針,則必須自己實現 IEnumerator。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.