【发布时间】:2019-03-02 10:05:00
【问题描述】:
现在,我知道在 C# 中,数组是一个固定大小的集合。你不能对它们使用RemoveAt 方法是有道理的,except that the System.Array class, which all array types extend from, implements the System.Collections.IList interface,这需要RemoveAt 方法。
如果是you upcast an array to an IList, or pass it to a method taking an IList as an argument,你可以调用.RemoveAt,它会在运行时抛出一个NotSupportedException。但是,如果我不向上转换它,and call it directly,它将导致 编译器错误,而不是 'int[]' does not contain a definition for 'RemoveAt',尽管该方法显然存在。
什么允许编译器在编译时捕获这个NotSupportedException?它是数组的特殊情况,还是我可以定义自己的类来具有这种行为?
【问题讨论】:
-
System.Array将RemoveAt实现为显式接口实现,即IList.RemoveAt,因此它不会仅按名称公开。
标签: c# arrays compiler-errors