arrays in c#

By | February 8, 2019

Arrays in c# programming language-:  An array is a data structure used for storing a collection of values that all have the same data type.An array is a data structure that contains a number of variables that are accessed through computed indices. The variables contained in an array, also called the elements of the array, are all of the same type, and this type is called the element type of the array.

An array has a rank that determines the number of indices associated with each array element. The rank of an array is also referred to as the dimensions of the array. An array with a rank of 1 is called a single-dimensional array. An array with a rank greater than 1 is called a multi-dimensional array. Specific-sized multi-dimensional arrays are often referred to as two-dimensional arrays, three-dimensional arrays, and so on.

Each dimension of an array has an associated length that is an integral number greater than or equal to zero. The dimension lengths are not part of the type of the array, but rather are established when an instance of the array type is created at Runtime. The length of a dimension determines the valid range of indices for that dimension: For a dimension of length N, indices can range from 0 to N – 1 inclusive. The total number of elements in an array is the product of the lengths of each dimension in the array. If one or more of the dimensions of an array have a length of zero, the array is said to be empty.

Array Declaration
To declare an array, a set of square brackets is appended to the data type that the array will contain, followed by the array’s name. An array can be declared with any data type and all of its elements will then be of that type.
int[] x; // integer array

Array Allocation
The array is allocated with the new keyword, followed again by the data type and a set of square brackets containing the length of the array. This is the fixed number of elements that the array can contain. Once the array is created, the elements will automatically be assigned to the default value for that data type, in this case zero.
int[] x = new int[3];

Array Assignment
To fill the array elements, they can be referenced one at a time and then assigned values. An array element is referenced by placing the element’s index inside square brackets. Notice that the index for the first element starts with zero.
x[0] = 1;
x[1] = 2;
x[2] = 3;
Alternatively, the values can be assigned all at once by using a curly bracket notation. The new keyword and data type may optionally be left out if the array is declared at the same time.
int[] y = new int[] { 1, 2, 3 };
int[] z = { 1, 2, 3 };

Array Access
Once the array elements are initialized, they can be accessed by referencing the elements’ indexes inside the square brackets.
System.Console.Write(x[0] + x[1] + x[2]); // “6”

Rectangular Arrays-:
There are two kinds of multi-dimensional arrays in C#: rectangular and jagged. A rectangular array has the same length of all sub-arrays and separates the dimensions using a comma.
string[,] x = new string[2, 2];
As with single-dimensional arrays, they can either be filled in one at a time or all at once during the allocation.
x[0, 0] = “00”; x[0, 1] = “01”;
x[1, 0] = “10”; x[1, 1] = “11”;
string[,] y = { { “00”, “01” }, { “10”, “11” } };

Jagged Arrays-:
Jagged arrays are arrays of arrays, and they can have irregular dimensions. The dimensions are allocated one at a time and the sub-arrays can therefore be allocated to different sizes.
string[][] a = new string[2][];
a[0] = new string[1]; a[0][0] = “00”;
a[1] = new string[2]; a[1][0] = “10”; a[1][1] = “11”;
It is possible to assign the values during the allocation.
string[][] b = { new string[] { “00” },
new string[] { “10”, “11” } };
These are all examples of two-dimensional arrays. If we need more than two dimensions, more commas can be added for the rectangular array, or more square brackets for the jagged array .

 

Leave a Reply

Your email address will not be published. Required fields are marked *