很长一段时间以来,我对以下内容感到好奇:
int[] array = new int[1];
int iArrayLength = array.Length; //1
由于数组实现了 IList 接口(interface),因此允许:
int iArrayCount = ((IList<int>)array).Count; //still 1
但是:
int iArrayCount = array.Count; //Compile error. WHY?
int iArrayLength = array.Length; //This is what we learned at school!
问题:
数组如何实现 IList<T>
(尤其是 int Count { get; }
中的 IList<T>
属性)却不允许在基类上使用它?
这称为显式接口(interface)成员实现。接口(interface)成员不会作为类型的公共(public)成员公开,但可以通过转换对接口(interface)类型的引用来使用。
这可以在 C# 中像这样完成:
interface I
{
void M();
}
class C : I
{
public int P { get; set; }
void I.M() { Console.WriteLine("M!"); }
}
然后你可以像这样使用这些类型:
C obj = new C();
obj.P = 3;
((I)obj).M();
但这不会编译:
obj.M();
正如 JeffN825 指出的那样,显式实现接口(interface)成员的原因之一是类型不支持它们。例如,Add
会引发异常 (relevant discussion)。显式实现成员的另一个原因是它复制了另一个具有不同名称的公共(public)成员。这就是显式实现 Count
的原因;相应的公共(public)成员是Length。
最后,一些成员被隐式实现,即索引器。这两行都有效(假设 arr
是一个 int
数组):
arr[0] = 8;
((IList<int>)arr)[0] = 8;