简体   繁体   English

C#中的数组索引类型?

[英]Type of array index in C#?

What is the type of an array index in C#? C#中的数组索引的类型是什么?

For example, in the code below, would the index be cast in an int before accessing the array element (third line)? 例如,在下面的代码中,索引是否会在访问数组元素(第三行)之前转换为int?

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'? 如果不是,使用'byte'类型的索引而不是'int'类型的索引访问数组元素会更快吗?

Thanks 谢谢

Original answer: 原始答案:

Yes, it's always an int for an array access expression. 是的,它总是一个数组访问表达式的int Other indexers (eg in Dictionary<,> ) can have other parameter types, but an array access index is always int , via promotion if necessary (as per your example). 其他索引器(例如在Dictionary<,> )可以有其他参数类型,但是数组访问索引总是int ,必要时通过提升(根据您的示例)。

But wait! 可是等等!

Actually, looking at section 7.6.6.1 of the C# 5 specification, I'm not as sure: 实际上,看一下C#5规范的7.6.6.1节,我不太确定:

For an array access, the primary-no-array-creation-expression of the element-access must be a value of an array-type. 对于数组访问,element-access的primary-no-array-creation-expression必须是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. 此外,不允许数组访问的参数列表包含命名参数。参数列表中的表达式数必须与数组类型的等级相同,并且每个表达式必须是intuint类型, longulong ,或者必须可以隐式转换为这些类型中的一个或多个。 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. 评估数组访问的结果是数组的元素类型的变量,即由参数列表中的表达式的值选择的数组元素。

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 [A]形式的数组访问的运行时处理,其中P是数组类型的primary-no-array-creation-expression,A是参数列表,包括以下步骤:

  • P is evaluated. P被评估。 If this evaluation causes an exception, no further steps are executed. 如果此评估导致异常,则不执行进一步的步骤。

  • 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 . 在评估每个索引表达式之后,执行对以下类型之一的隐式转换(第6.1节): intuintlongulong 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. 例如,如果索引表达式的类型为short则执行对int的隐式转换,因为从shortint以及从shortlong隐式转换是可能的。 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. 如果对索引表达式的评估或后续的隐式转换导致异常,则不会评估其他索引表达式,也不会执行其他步骤。

And indeed this code works: 确实这段代码有效:

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

So while in your particular case the byte would be promoted to int , access index access isn't always through an int . 因此, 在您的特定情况下byte将被提升为int ,访问索引访问并不总是通过int

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. 即使.NET 4.5中支持“大型阵列”,我也不认为您可以创建一个包含多个int.MaxValue元素的数组,但我可能错了。 (I don't have time to test it right now, I'm afraid.) (我现在没时间测试它,我很害怕。)

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

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