如何在C#中创建一个1维数组,索引从1开始

对于多维数组,Array.CreateInstance可用于创建基于非零索引的数组,但如果您尝试使用1维数组(向量),例如:

public double[] myArray = (double[])Array.CreateInstance(typeof(double), new int[1] { 12 }, new int[1] { 1 }); 

当从多维数组转换为单维数组失败时,这将在运行时失败

 "Unable to cast object of type 'System.Double[*]' to type 'System.Double[]'" 

现在我可以创建一个基于零的数组并忽略第一个值,或者使用偏移等,但我是否忽略了一些允许基于非零的向量的c#语法魔法?

更新:

如果他说“ 没有明显的方法可以在C#中创建一个非零的数组”,我会接受Eric Lippert的话。

C#中的所有数组都是基于零的。 据我所知,没有办法创建一个基于1的数组。 想象一下,如果可能的话会发生什么样的混乱。 这是一个类似的线程,它解释了更多细节的问题 – C#:基于非零的数组不符合CLS

你可以在C#中创建一个非零的数组,但它的使用是有点令人讨厌的。 它绝对不是普通(即,基于零的单维)arrays的简单替代品。

  // Create the array. Array myArray = Array.CreateInstance(typeof(double), new int[1] { 12 }, new int[1] { 1 }); // Fill the array with random values. Random rand = new Random(); for (int index = myArray.GetLowerBound(0); index <= myArray.GetUpperBound(0); index++) { myArray.SetValue(rand.NextDouble(), index); } // Display the values. for (int index = myArray.GetLowerBound(0); index <= myArray.GetUpperBound(0); index++) { Console.WriteLine("myArray[{0}] = {1}", index, myArray.GetValue(index)); } 

为此所需的GetValue / SetValue语法比在每次出现时从向量索引中减去一个语法更加丑陋。

如果值类型存储在数组中,那么它将被存储在连续位置,就像在常规数组中一样,但是getter和setter将需要装箱值(除非有一些我不知道的编译器魔法) 。 吸气剂通常需要一个演员(只是为了使它更加丑陋)。

  double myValue = (double)myArray.GetValue(index); 

另请注意, GetUpperBound的正确比较是<= ,与Length相比, <

基于非零的数组DO存在于C中,并且有一种方法可以创建基于1(或其他)的数组。

我完全同意它们是混乱的,并且它们不应该用于除遗留内容之外的任何东西,但它们与旧的COM库交互是必不可少的。

遇到这种情况最常见的地方是使用Excel库中的Microsoft.Office.Interop.Excel.Range对象,该对象仍然使用下面的旧DCOM接口。

例:

 ///  /// Makes the equivalent of a local Excel range that can be populated /// without leaving .net ///  /// number of rows in the table /// number of columns in the table /// a 1's based, 2 dimensional object array which can put back to Excel in one DCOM call. public static object[,] NewObjectArray(int iRows, int iCols) { int[] aiLowerBounds = new int[] { 1, 1 }; int[] aiLengths = new int[] { iRows, iCols}; return (object[,])Array.CreateInstance(typeof(object), aiLengths, aiLowerBounds); } 

在这种情况下,这个代码是必要的原因是每个DCOM调用excel是一个跨进程调用,如果你一次一个地访问单元格,你会产生巨大的开销,(检索或设置)值)。 Excel范围是基于1的2维数组,如果创建数组并在本地填充数组,则可以将其推送到一个跨进程调用中,从而创建巨大的性能提升。