很长一段时间我都对以下内容感到好奇:
int[] array = new int[1];
int iArrayLength = array.Length; //1
Run Code Online (Sandbox Code Playgroud)
由于数组实现了IList接口,因此允许以下内容:
int iArrayCount = ((IList<int>)array).Count; //still 1
Run Code Online (Sandbox Code Playgroud)
但:
int iArrayCount = array.Count; //Compile error. WHY?
int iArrayLength = array.Length; //This is what we learned at school!
Run Code Online (Sandbox Code Playgroud)
问题:如何IList<T>在不允许在基类上使用数组的情况下实现(特别是int Count { get; }属性IList<T>)?