Arrays In C Sharp



1) An array is a collection of contiguous data members that have the common name.
2) Whenever we want to set or get a value to or from an arrays, we use its index value.
3) Array are always starts at index zero.
4) Means the first data member of an array starts at the 0th index and last data member at total number of member - 1.
5) For example :- If an array has 5 elements. The first element at 0 index and last at 4 index.
6) We can create a fixed sized array(in which size is predefined) or dynamic sized array(in which size is not predefined).
7) We can divided an array into three types.
a) Single-dimensional arrays
b) Multidimensional arrays
c) Array-of-arrays (jagged)

--------------------------------------------------------------------------------------------------------
Declaration of array
Single-dimensional arrays:-    int[] numbers;
Multidimensional arrays:-      string[,] names;
Array-of-arrays (jagged):-     byte[][] scores;

Different ways to initialize single-dimensional array 
int[] numbers = new int[5] {1, 2, 3, 4, 5};
string[] names = new string[3] {"Amit", "Gopal", "Rajat"};

Note : - You can omit the size of the array, like this:

int[] numbers = new int[] {1, 2, 3, 4, 5};
string[] names = new string[] {"Amit", "Gopal", "Rajat"};

Note :- You can also omit the new operator if an initializer is provided, like this:

int[] numbers = {1, 2, 3, 4, 5};
string[] names = {"Amit", "Gopal", "Rajat"};

Different ways to initialize multi-dimensional array
int[,] numbers = new int[3, 2] { {1, 2}, {3, 4}, {5, 6} };
string[,] siblings = new string[2, 2] { {"Amit","Ami"}, {"Gopal","Gopi"} };

Note : - You can omit the size of the array, like this:

int[,] numbers = new int[,] { {1, 2}, {3, 4}, {5, 6} };
string[,] siblings = new string[,] { {"Amit","Ami"}, {"Gopal","Gopi"} };

Note :- You can also omit the new operator if an initializer is provided, like this:

int[,] numbers = { {1, 2}, {3, 4}, {5, 6} };
string[,] siblings = { {"Amit","Ami"}, {"Gopal","Gopi"} };

Different ways to initialize jagged array
int[][] numbers = new int[2][] { new int[] {2,3,4}, new int[] {5,6,7,8,9} };

Note : - You can also omit the size of the first array, like this:

int[][] numbers = new int[][] { new int[] {2,3,4}, new int[] {5,6,7,8,9} };

Note :- You can also omit the new operator for the first array, if an initializer is provided, like this:

int[][] numbers = { new int[] {2,3,4}, new int[] {5,6,7,8,9} };

---------------------------------------------------------------------------------------------------

Arrays In C Sharp Arrays In C Sharp Reviewed by Baljeet Singh on 08:08 Rating: 5

No comments:

Powered by Blogger.