基于非零的多维数组 [英] Non-zero based multidimensional arrays

查看:66
本文介绍了基于非零的多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Interopt.Excel API从电子表格中提取单元格.当我打电话时:

I am extracting cells out of a spreadsheet using the Interopt.Excel API. When I call:

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

并设置一个断点并检查 rangeValues ,我看到以[1,1]"foo",[1,2]"bar"等开头的元素.

And set a breakpoint and inspect rangeValues, I see elements starting at [1,1] "foo", [1,2] "bar", etc.

但是,如果我执行 string [,] test = new string [2,2] {{一个",两个"},{三个",四个"}};

元素从[0,0]开始.Excel API如何构造带有空元素的多维数组?我尝试添加null,但是您仍然有[0,0]条目.他们的对象没有显示出来.

The elements start at [0,0]. How does the Excel API construct a multidimensional array w/ empty elements? I tried adding null but you still have an [0,0] entry. Their object doesn't show that.

推荐答案

CLR支持基于非零的数组.它们令人困惑,必须避免.我相信,COM互操作编组可以创建它们.

The CLR supports non-zero based arrays. They are confusing and to be avoided. I believe, the COM interop marshaller can create them.

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

符合公共语言规范(CLS)要求所有数组从零开始.这允许使用C#编写的方法来创建数组,并将数组的引用传递给用另一种语言编写的代码语言,例如VisualBasic®.另外,由于从零开始的数组是迄今为止最常见的,Microsoft花了很多时间优化其性能.但是,CLR确实支持非基于零的数组,但不建议使用.对于那些你们不在乎性能和跨语言可移植性,我会稍后在此演示如何创建和使用基于非零的数组部分.

Common Language Specification (CLS) compliance requires that all arrays be zero-based. 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®. In addition, since zero-based arrays are by far the most common, Microsoft has spent a lot of time optimizing their performance. However, the CLR does support non-zero-based arrays but they are discouraged. 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.

显然,多维数组无论是否基于零都具有相同的类型.一维的具有不同的类型.这是有道理的,因为如果它们具有相同的类型,那么JIT通常将不得不为一般情况生成缓慢的代码.

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.

这篇关于基于非零的多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆