为什么 C# 数组没有 Count 属性?

发布于 2024-10-13 18:10:22 字数 525 浏览 2 评论 0原文

可能的重复:
集合中的计数、长度与大小

真的很奇怪:

C#如下所示的数组

double[] test = new double[1];

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

IList<double> list = test;

但是,IList 接口还提供了 Count 属性。为什么数组(在本例中为“测试”)没有?

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

Possible Duplicate:
count vs length vs size in a collection

Really strange:

C# arrays such as the following

double[] test = new double[1];

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

IList<double> list = test;

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

嘦怹 2024-10-20 18:10:22

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

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

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

int ICollection.Count { get { return Length; } }
电影里的梦 2024-10-20 18:10:22

这是关于命名的设计选择,而不是语义。

数组具有 Length 属性,字符串也是如此。

长度信号不可变:您不能添加到数组或从数组中删除。

列表和其他容器具有通常可以更改的 Count 属性。

哦,如果您调用 list.Append(1.1); 您将收到不支持的异常。

It was a design choice about Naming, not semantics.

Arrays have a Length property, as does the String.

Length signals immutable: You cannot Add to or Remove from an array.

Lists and other containers have a Count property that can usually change.

Oh, and if you call list.Append(1.1); you will get a not supported exception.

谁与争疯 2024-10-20 18:10:22

Count 属性使用显式接口声明样式隐藏,例如在类定义中:

int IList.Count {
    get {
        // ...etc...
    }
}

您可以使用类型转换访问像这样隐藏的方法和属性,例如

((IList<double>) myArray).Count

The Count property is hidden using the explicit interface declaration style, for example like this in a class definition:

int IList.Count {
    get {
        // ...etc...
    }
}

You can access methods and properties hidden like this using a type cast, e.g.

((IList<double>) myArray).Count
追星践月 2024-10-20 18:10:22

继承自 Array 的类型获取的实现>IList 在运行时(这是怎么可能的,不要问我):

在 .NET Framework 2.0 版中,
Array 类实现了
System.Collections.Generic.IList,
System.Collections.Generic.ICollection,

System.Collections.Generic.IEnumerable
通用接口。
提供给数组的实现
在运行时
,因此不是
对文档构建可见
工具。结果,通用的
界面中没有出现
Array 的声明语法
类,并且没有参考
接口成员的主题是
只能通过将数组转换为
通用接口类型(显式
接口实现)。关键
当你施放时要注意的事情
这些接口之一的数组是
添加、插入或
删除元素抛出
NotSupportedException

实际上,IList 实现的行为类似于 显式实现,如 Marc 解释的 在他的回答中。这就是为什么您可以从强制转换的结果访问 IList 的某些成员,但不能从专门键入为 T[] 的变量访问。

Types inheriting from Array obtain implementations of IList<T> at run-time (how this is possible, don't ask me):

In the .NET Framework version 2.0, the
Array class implements the
System.Collections.Generic.IList<T>,
System.Collections.Generic.ICollection<T>,
and
System.Collections.Generic.IEnumerable<T>
generic interfaces. The
implementations are provided to arrays
at run time
, and therefore are not
visible to the documentation build
tools. As a result, the generic
interfaces do not appear in the
declaration syntax for the Array
class, and there are no reference
topics for interface members that are
accessible only by casting an array to
the generic interface type (explicit
interface implementations). The key
thing to be aware of when you cast an
array to one of these interfaces is
that members which add, insert, or
remove elements throw
NotSupportedException.

In effect the IList<T> implementation acts like an explicit implementation, as Marc explained in his answer. This is why you can access certain members of IList<T> from the result of a cast but not from a variable typed as T[] specifically.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文