.Net 具有用于非零基礎(chǔ)數(shù)組的特殊內(nèi)部類型。C# 語言沒有此類型的語法,因此無法強(qiáng)制轉(zhuǎn)換為此類型。但是 2d 非從零開始的數(shù)組具有普通數(shù)組類型。我想知道為什么只有一維數(shù)組有特殊類型?var array1 = (int[]) Array.CreateInstance( typeof( int ), new[] { 6 }, new[] { -1 } ); // System.InvalidCastException : Unable to cast object of type 'System.Int32[*]' to type 'System.Int32[]'var array2 = (int[,]) Array.CreateInstance( typeof( int ), new[] { 6, 6 }, new[] { -1, -1 } ); // it works
1 回答

神不在的星期二
TA貢獻(xiàn)1963條經(jīng)驗(yàn) 獲得超6個(gè)贊
從 0 開始的一維數(shù)組很特殊。它是一個(gè)“向量數(shù)組”。它們由 CLR 進(jìn)行了大量優(yōu)化。
任何不是從 0 開始或不是一維的數(shù)組都不是向量數(shù)組。
因此,an 不是向量數(shù)組,因此第二次強(qiáng)制轉(zhuǎn)換有效。
并且是真正不同的類型。int[,]
int[*]
int[]
- 1 回答
- 0 關(guān)注
- 208 瀏覽
添加回答
舉報(bào)
0/150
提交
取消