小编Ger*_*rit的帖子

如何在没有在C#中实现属性"Count"的情况下实现IList <T>?

很长一段时间我都对以下内容感到好奇:

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>)?

c# arrays list

25
推荐指数
2
解决办法
1250
查看次数

标签 统计

arrays ×1

c# ×1

list ×1