我意識到我們不能在C#中使用靜態索引器。但爲什麼下面的代碼能夠正確編譯(在C#4.0下)?C#靜態索引器不應編譯,但是
由於Fred是一個靜態類,它甚至不能被實例化。聲明的靜態索引器沒有意義,但編譯器允許它。爲什麼?我無法想象這是一個編譯器錯誤,這是遲到語言。
public static class Fred {
public static int this[String str] {
get {
if (str != null)
return str.Length;
return -1;
}
}
}
在4.0 。 – 2011-06-03 18:11:05
「'Fred.this [string]':無法在靜態類中聲明索引器」和「修飾符'static'對此項無效」。 – sisve 2011-06-03 18:11:52