大家好!假设我们有下面列出的 3 个索引器方法。为什么在运行时运行带有 this[int i] 参数的方法而不是 this[byte i] 或 this[long i] 呢?
MyType mType = new MyType();
Console.WriteLine($"{mType[0]}");
public sealed class MyType {
public string this[byte i]{
get {
return "Indexer byte";
}
set{
value = "Indexer byte";
}
}
public string this[int i]{
get{
return "Indexer int";
}
set{
value = "Indexer int";
}
}
public string this[long i]{
get{
return "Indexer long";
}
set{
value = "Indexer long";
}
}
}
要获取您想要的类型,请使用您想要的文字。
结果是这样的