我意识到我们不能在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