在C#中,我了解到无法创建接口的实例:
> new IList<double>()
(1,1): error CS0144: Cannot create an instance of the abstract class or interface 'IList<double>'
但是今天我看到了以下代码:
> new IList<double>[3]
IList<double>[3] { null, null, null }
那不是很奇怪!这怎么可能?
特别是当double为不可为空时,null如何成为元素?
> new List<double> { null }
(1,20): error CS1950: The best overloaded Add method 'List<double>.Add(double)' for the collection initializer has some invalid arguments
(1,20): error CS1503: Argument 1: cannot convert from '<null>' to 'double'
这是怎么回事?
答案 0 :(得分:8)
您所拥有的是一个IList数组,其中充满了null。考虑:
~/go/
答案 1 :(得分:4)
new IList<double>[3]
创建一个new IList<double>[]
,也就是说每个元素new IList<double>
的数组。
例如,您可以这样做:
var arr = new IList<double>[3];
arr[0] = new List<double>();
答案 2 :(得分:0)
仔细查看输出,可以区分一维和二维数据结构。
一个简单的列表(一维):
> new List<string> { null, null, null }
List<string>(3) { null, null, null }
列表数组(二维):
> new IList<string>[3]
IList<string>[3] { null, null, null }
在方括号中,3表示数组的长度。在圆括号中,3表示列表的长度。