Array Initialization
Array Initialization
You can declare a single-dimensional array of five integers as shown in the following
example:
This array contains the elements from array[0] to array[4]. The new operator is used to
create the array and initialize the array elements to their default values. In this example,
all the array elements are initialized to zero.
An array that stores string elements can be declared in the same way. For example:
Array Initialization
It is possible to initialize an array upon declaration, in which case, the rank specifier is
not needed because it is already supplied by the number of elements in the initialization
list. For example:
A string array can be initialized in the same way. The following is a declaration of a
string array where each array element is initialized by a name of a day:1
When you initialize an array upon declaration, you can use the following shortcuts:+
int[] array2 = { 1, 3, 5, 7, 9 };
It is possible to declare an array variable without initialization, but you must use the new
operator when you assign an array to this variable. For example:
int[] array3;
array3 = new int[] { 1, 3, 5, 7, 9 }; // OK
The result of this statement depends on whether sometype is a value type or a reference
type. If it is a value type, the statement creates an array of 10 elements, each of which
has the type sometype. If sometype is a reference type, the statement creates an array
of 10 elements, each of which is initialized to a null reference.
Multidimensional array:
Arrays can have more than one dimension. For example, the following declaration
creates a two-dimensional array of four rows and two columns.
Array Initialization
You can initialize the array upon declaration, as is shown in the following example.
// Two-dimensional array.
int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
// The same array with dimensions specified.
int[,] array2Da = new int[4, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
// A similar array with string elements.
string[,] array2Db = new string[3, 2] { { "one", "two" }, { "three", "four" },
{ "five", "six" } };
// Three-dimensional array.
int[, ,] array3D = new int[,,] { { { 1, 2, 3 }, { 4, 5, 6 } },
{ { 7, 8, 9 }, { 10, 11, 12 } } };
// The same array with dimensions specified.
int[, ,] array3Da = new int[2, 2, 3] { { { 1, 2, 3 }, { 4, 5, 6 } },
{ { 7, 8, 9 }, { 10, 11, 12 } } };
// Accessing array elements.
System.Console.WriteLine(array2D[0, 0]);
System.Console.WriteLine(array2D[0, 1]);
System.Console.WriteLine(array2D[1, 0]);
System.Console.WriteLine(array2D[1, 1]);
System.Console.WriteLine(array2D[3, 0]);
System.Console.WriteLine(array2Db[1, 0]);
System.Console.WriteLine(array3Da[1, 0, 1]);
System.Console.WriteLine(array3D[1, 1, 2]);
// Output:
// 1
// 2
// 3
// 4
// 7
// three
// 8
// 12
// 12 equals 12
You also can initialize the array without specifying the rank.
int[,] array4 = { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
If you choose to declare an array variable without initialization, you must use the new
operator to assign an array to the variable. The use of new is shown in the following
example
int[,] array5;
array5 = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } }; // OK
array5[2, 1] = 25;
Similarly, the following example gets the value of a particular array element and assigns
it to variable elementvalue
The following code example initializes the array elements to default values (except for
jagged arrays).
jagged array:
A jagged array is an array whose elements are arrays. The elements of a jagged array
can be of different dimensions and sizes. A jagged array is sometimes called an "array of
arrays." The following examples show how to declare, initialize, and access jagged
arrays.
General form:
Initialization of jaggedarray
It is also possible to use initializers to fill the array elements with values, in which case
you do not need the array size. For example:
You can also initialize the array upon declaration like this:+
int[][] arr =
{
new int[] {1,3,5,7,9},
new int[] {0,2,4,6},
new int[] {11,22}
};
A jagged array is an array of arrays, and therefore its elements are reference types and are
initialized to NULL.
};
You can access individual elements as shown in this example, which displays the value of the
element [1,0] of the first array value 5`
The method Length returns the number of arrays contained in the jagged array. For
example, assuming you have declared the previous array, this line:
System.Console.write( “value is” arr.Length);
returns a value of 3.
Example
This example builds an array whose elements are themselves arrays. Each one of the
array elements has a different size.
class ArrayTest
{
static void Main()
{
// Declare the array of two elements:
int[][] arr = new int[2][];