在C#中的数组索引类型? [英] Type of array index in C#?

查看:248
本文介绍了在C#中的数组索引类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是数组索引在C#中的类型?

What is the type of an array index in C#?

例如,在下面的code,将索引中的一个int投访问数组元素(第三行)之前?

For example, in the code below, would the index be cast in an int before accessing the array element (third line)?

T[] myArray = new T[255];
byte index = 2;
T element = myArray[index];

如果不是,它是速度更快通过使用类型字节的指数比类型'诠释'?

If not, is it faster to access an array element by using an index of type 'byte' than an index of type 'int'?

感谢

推荐答案

原来的答复:

是的,它总是一个 INT 对于数组访问前pression。其他索引(例如,在词典<,> )可以有其他的参数类型,而是一个数组访问索引始终是 INT ,通过推广(如有必要,按你的例子​​)。

Yes, it's always an int for an array access expression. Other indexers (e.g. in Dictionary<,>) can have other parameter types, but an array access index is always int, via promotion if necessary (as per your example).

别急!

其实,看着部分C#5规范的7.6.6.1,我没有那么肯定的:

Actually, looking at section 7.6.6.1 of the C# 5 specification, I'm not as sure:

有关数组访问的元素访问的基本没有数组创建前pression必须是一个数组类型的值。此外,数组访问参数列表不允许包含名为arguments.The在参数列表前pressions的数量必须相同数组类型的秩,每前pression的类型必须是 INT UINT ULONG ,或者必须是隐式转换为一个或多个类型。
  评价一个数组访问的结果是数组,即通过在参数列表除权pression(S)的值(S)选择的数组元素的元素类型的变量。

For an array access, the primary-no-array-creation-expression of the element-access must be a value of an array-type. Furthermore, the argument-list of an array access is not allowed to contain named arguments.The number of expressions in the argument-list must be the same as the rank of the array-type, and each expression must be of type int, uint, long, ulong, or must be implicitly convertible to one or more of these types. The result of evaluating an array access is a variable of the element type of the array, namely the array element selected by the value(s) of the expression(s) in the argument-list.

形式P [A]的一个数组访问的运行时处理,其中P是一个初级的无数组创建前$ P $一个数组类型的pssion,A是一个参数列表,包括以下步骤:

The run-time processing of an array access of the form P[A], where P is a primary-no-array-creation-expression of an array-type and A is an argument-list, consists of the following steps:


      
  • P 进行评估。如果此计算导致异常,则不执行进一步的操作。

  • P is evaluated. If this evaluation causes an exception, no further steps are executed.

参数列表中的索引前pressions为了进行评估,从左至右。在每次指数前pression的评价,以下类型之一的隐式转换(第6.1节)执行: INT UINT ULONG 。在此列表中的量的隐式转换存在第一类型的选择。例如,如果该指数前pression类型为然后进行隐式转换为 INT ,因为从来隐式转换 INT 是可能的。如果索引前pression或后面的隐式转换时导致异常,则没有进一步的指数前pressions进行评估,并不再执行进一步的步骤。

The index expressions of the argument-list are evaluated in order, from left to right. Following evaluation of each index expression, an implicit conversion (§6.1) to one of the following types is performed: int, uint, long, ulong. The first type in this list for which an implicit conversion exists is chosen. For instance, if the index expression is of type short then an implicit conversion to int is performed, since implicit conversions from short to int and from short to long are possible. If evaluation of an index expression or the subsequent implicit conversion causes an exception, then no further index expressions are evaluated and no further steps are executed.

这确实code工作:

string[] array = new string[10];
long index = 10;
string element = array[index];

因此​​,虽然的你的具体情况的的字节将被提升到 INT ,访问索引访问并不总是通过 INT

So while in your particular case the byte would be promoted to int, access index access isn't always through an int.

即使在.NET 4.5的大阵列的支持,我不知道的认为的,你可以创建一个比 int.MaxValue 元素,但我可能是错的。 (我没有时间,现在,恐怕对它进行测试。)

Even with the "large arrays" support in .NET 4.5, I don't think that you can create an array with more than int.MaxValue elements, but I could be wrong. (I don't have time to test it right now, I'm afraid.)

这篇关于在C#中的数组索引类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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