Arrays
You can store multiple variables of the same type in an array data structure. You declare an array by specifying the type of its elements. If you want the array to store elements of any type, you can specify object
as its type. In the unified type system of C#, all types, predefined and user-defined, reference types and value types, inherit directly or indirectly from Object.
// Declare array with explicit size (size is 2)
int[] twoInts = new int[2];
// Assign second element by index
twoInts[1] = 8;
// Retrieve the second element by index
twoInts[1] == 8; // => true
Arrays can also be defined using a shortcut notation that allows you to both create the array and set its value. As the compiler can now tell how many elements the array will have, the length can be omitted:
// Three equivalent ways to declare and initialize an array (size is 3)
int[] threeIntsV1 = new int[] { 4, 9, 7 };
int[] threeIntsV2 = new[] { 4, 9, 7 };
int[] threeIntsV3 = { 4, 9, 7 };
Arrays can be manipulated by either calling an array instance's methods or properties, or by using the static methods defined in the Array
class.