Arrays As Objects (C# Programming Guide) : Example
Arrays As Objects (C# Programming Guide) : Example
In C#, arrays are actually objects, and not just addressable regions of contiguous memory as in C and C++. Array is
the abstract base type of all array types. You can use the properties, and other class members, that Array has. An
example of this would be using the Length property to get the length of an array. The following code assigns the
length of the numbers array, which is 5 , to a variable called lengthOfNumbers :
int[] numbers = { 1, 2, 3, 4, 5 };
int lengthOfNumbers = numbers.Length;
The Array class provides many other useful methods and properties for sorting, searching, and copying arrays.
Example
This example uses the Rank property to display the number of dimensions of an array.
class TestArraysClass
{
static void Main()
{
// Declare and initialize an array:
int[,] theArray = new int[5, 10];
System.Console.WriteLine("The array has {0} dimensions.", theArray.Rank);
}
}
// Output: The array has 2 dimensions.
See Also
C# Programming Guide
Arrays
Single-Dimensional Arrays
Multidimensional Arrays
Jagged Arrays
Single-Dimensional Arrays (C# Programming Guide)
11/21/2017 • 1 min to read • Edit Online
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:
When you initialize an array upon declaration, you can use the following shortcuts:
int[] array2 = { 1, 3, 5, 7, 9 };
string[] weekDays2 = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
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
//array3 = {1, 3, 5, 7, 9}; // Error
C# 3.0 introduces implicitly typed arrays. For more information, see Implicitly Typed Arrays.
See Also
Array
C# Programming Guide
Arrays
Multidimensional Arrays
Jagged Arrays
Multidimensional Arrays (C# Programming Guide)
11/21/2017 • 2 min to read • Edit Online
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 } } };
// 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 = {{1,2}, {3,4}, {5,6}, {7,8}}; // Error
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).
See Also
C# Programming Guide
Arrays
Single-Dimensional Arrays
Jagged Arrays
Passing Arrays as Arguments (C# Programming
Guide)
11/21/2017 • 3 min to read • Edit Online
Arrays can be passed as arguments to method parameters. Because arrays are reference types, the method can
change the value of the elements.
int[] theArray = { 1, 3, 5, 7, 9 };
PrintArray(theArray);
You can initialize and pass a new array in one step, as is shown in the following example.
Example
Description
In the following example, an array of strings is initialized and passed as an argument to a PrintArray method for
strings. The method displays the elements of the array. Next, methods ChangeArray and ChangeArrayElement are
called to demonstrate that sending an array argument by value does not prevent changes to the array elements.
Code
class ArrayClass
{
static void PrintArray(string[] arr)
{
for (int i = 0; i < arr.Length; i++)
{
System.Console.Write(arr[i] + "{0}", i < arr.Length - 1 ? " " : "");
}
System.Console.WriteLine();
}
// Print the array again, to verify that it has not been changed.
System.Console.WriteLine("Array weekDays after the call to ChangeArray:");
PrintArray(weekDays);
System.Console.WriteLine();
int[,] theArray = { { 1, 2 }, { 2, 3 }, { 3, 4 } };
Print2DArray(theArray);
The following code shows a partial declaration of a print method that accepts a two-dimensional array as its
argument.
void Print2DArray(int[,] arr)
{
// Method code.
}
You can initialize and pass a new array in one step, as is shown in the following example.
Example
Description
In the following example, a two-dimensional array of integers is initialized and passed to the Print2DArray method.
The method displays the elements of the array.
Code
class ArrayClass2D
{
static void Print2DArray(int[,] arr)
{
// Display the array elements.
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr.GetLength(1); j++)
{
System.Console.WriteLine("Element({0},{1})={2}", i, j, arr[i, j]);
}
}
}
static void Main()
{
// Pass the array as an argument.
Print2DArray(new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } });
See Also
C# Programming Guide
Arrays
Single-Dimensional Arrays
Multidimensional Arrays
Jagged Arrays