简体   繁体   English

基于非零的多维数组

[英]Non-zero based multidimensional arrays

I am extracting cells out of a spreadsheet using the Interopt.Excel API. 我正在使用Interopt.Excel API从电子表格中提取单元格。 When I call: 我打电话的时候:

object[,] rangeValues = (object[,])range.get_Value(XlRangeValueDataType.xlRangeValueDefault);

And set a breakpoint and inspect rangeValues , I see elements starting at [1,1] "foo", [1,2] "bar", etc. 并设置断点并检查rangeValues ,我看到元素从[1,1]“foo”开始,[1,2]“bar”等。

However, if I do string[,] test = new string[2, 2] { { "one", "two" }, { "three", "four" } }; 但是,如果我做string[,] test = new string[2, 2] { { "one", "two" }, { "three", "four" } };

The elements start at [0,0]. 元素从[0,0]开始。 How does the Excel API construct a multidimensional array w/ empty elements? Excel API如何使用空元素构造多维数组? I tried adding null but you still have an [0,0] entry. 我尝试添加null但你仍然有一个[0,0]条目。 Their object doesn't show that. 他们的目标没有显示出来。

The CLR supports non-zero based arrays. CLR支持基于非零的数组。 They are confusing and to be avoided. 它们令人困惑,无法避免。 I believe, the COM interop marshaller can create them. 我相信,COM interop marshaller可以创建它们。

http://msdn.microsoft.com/en-us/magazine/cc301755.aspx http://msdn.microsoft.com/en-us/magazine/cc301755.aspx

Common Language Specification (CLS) compliance requires that all arrays be zero-based. 公共语言规范(CLS)合规性要求所有阵列都是从零开始的。 This allows a method written in C# to create an array and pass the array's reference to code written in another language such as Visual Basic®. 这允许用C#编写的方法创建一个数组,并将数组的引用传递给用另一种语言(如VisualBasic®)编写的代码。 In addition, since zero-based arrays are by far the most common, Microsoft has spent a lot of time optimizing their performance. 此外,由于基于零的阵列是迄今为止最常见的,因此Microsoft花费了大量时间来优化其性能。 However, the CLR does support non-zero-based arrays but they are discouraged. 但是,CLR确实支持非零数组,但不鼓励使用它们。 For those of you who do not care about performance and cross-language portability, I will demonstrate how to create and use non-zero-based arrays later in this section. 对于那些不关心性能和跨语言可移植性的人,我将在本节后面演示如何创建和使用非零数组。

This is the first time I have seen one in real-world code. 这是我第一次在真实世界的代码中看过一个。

Apparently, multi-dimensional arrays have the same type no matter whether they are zero-based or not. 显然,多维数组具有相同的类型,无论它们是否为零。 Single-dimensional ones have a different type. 单维的有不同的类型。 Makes sense because if they had the same type the JIT would always have to produce slow code for the general case. 有道理,因为如果它们具有相同的类型,JIT总是必须为一般情况生成慢速代码。

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM