为什么C#数组没有Count属性? [英] Why does C# array not have Count property?

查看:331
本文介绍了为什么C#数组没有Count属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

计数与长度对比集合中的大小

真奇怪:

C#数组,如下所示

double[] test = new double[1];

支持Length属性以获取数组的大小。但是数组也实现了IList接口:

support the Length property to get the size of the array. But arrays also implement an IList interface:

IList<double> list = test;

但是,IList接口还提供了Count属性。为什么阵列(在这种情况下为测试)没有?

However, the IList interface provides also a Count property. How come the array ("test" in this case) doesn't?

编辑:感谢所有指出它的人实际上是提供Count属性的ICollection接口(不是IList),而且这是由于接口的显式实现。

Edit: Thanks to all of you who pointed out that it is in fact the ICollection interface (not IList) which provides the Count property, and also that this is due to explicit implementation of the interface.

推荐答案

简单地说,他们选择将其称为长度,并通过显式接口实现实现 Count - 类似于:

Simply, they chose to call it Length, and implement Count via explicit interface implementation -something like:

int ICollection.Count { get { return Length; } }

这篇关于为什么C#数组没有Count属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆