【发布时间】:2019-06-03 00:33:58
【问题描述】:
创建数组几乎是编程中最常见和最简单的任务,但在我深入了解 C# 之后,我发现除了直觉之外还有其他东西。我们都知道创建一个数组,我们可以:
int[] myInts = new int[3];
myInts[0] = 1;
myInts[1] = 2;
myInts[2] = 3;
int[] myAnotherInts = new int[] { 1, 2, 3,}; // Array Initialization Syntax
但我们不能这样做:
int[] myInts = new int[3](); // why we can't add () in the end?
虽然我们确实在集合上使用 () 作为:
List<int> myInts = new List<int>() { 1, 2, 3 };
问题 1:
为什么最后不能加()?
问题 2:
如果我的问题 1 的答案是 Array 是一个抽象类:
public abstract class Array : ICloneable, IList, ICollection, IEnumerable...
但是也使用了'new'关键字,表示调用了构造函数,表示创建了一个实例,这不违反抽象类规则不允许创建实例吗?
【问题讨论】:
-
附言:试试
List<int> myInts = new List<int> { 1, 2, 3 }; -
// why we can't add () in the end?因为语言语法说你不能。没有比这更好的答案了。