Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type of array index in C#?

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

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'?

Thanks

like image 813
Casto Avatar asked May 10 '13 16:05

Casto


People also ask

What is the type of array index?

An array is an indexed collection of component variables, called the elements of the array. The indexes are the values of an ordinal type, called the index type of the array. The elements all have the same size and the same type, called the element type of the array.

What type is the index of an array in C?

Arrays in C are indexed starting at 0, as opposed to starting at 1. The first element of the array above is point[0]. The index to the last value in the array is the array size minus one.

What are the 3 types of arrays?

There are three different kinds of arrays: indexed arrays, multidimensional arrays, and associative arrays.

What are the types of array in C?

Array in C are of two types; Single dimensional arrays and Multidimensional arrays.


1 Answers

Original answer:

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).

But wait!

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

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.

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 is evaluated. 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. 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.

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.

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.)

like image 189
Jon Skeet Avatar answered Oct 05 '22 22:10

Jon Skeet