2d array to list c#
C# | Arrays
An array is a group of like-typed variables that are referred to by a common name. And each data item is called an element of the array. The data types of the elements may be any valid data type like char, int, float, etc. and the elements are stored in a contiguous location. Length of the array specifies the number of elements present in the array. In C# the allocation of memory for the arrays is done dynamically. And arrays are kinds of objects, therefore it is easy to find their size using the predefined functions. The variables in the array are ordered and each has an index beginning from 0. Arrays in C# work differently than they do in C/C++. Important Points to Remember About Arrays in C#
The array has can contain primitive data types as well as objects of a class depending on the definition of an array. Whenever use primitives data types, the actual values have to be stored in contiguous memory locations. In the case of objects of a class, the actual objects are stored in the heap segment. The following figure shows how array stores values sequentially : Explanation : Array Declaration Syntax : < Data Type > [ ] < Name_Array >Here, Example : int[] x; // can store int values string[] s; // can store string values double[] d; // can store double values Student[] stud1; // can store instances of Student class which is custom classNote : Only Declaration of an array doesnt allocate memory to the array. For that array must be initialized. Array Initialization As said earlier, an array is a reference type so the new keyword used to create an instance of the array. We can assign initialize individual array elements, with the help of the index. Syntax : type [ ] < Name_Array > = new < datatype > [size];Here, type specifies the type of data being allocated, size specifies the number of elements in the array, and Name_Array is the name of an array variable. And new will allocate memory to an array according to its size. Examples : To Show Different ways for the Array Declaration and Initialization Example 1 : // defining array with size 5. // But not assigns values int[] intArray1 = new int[5];The above statement declares & initializes int type array that can store five int values. The array size is specified in square brackets([]). Example 2 : // defining array with size 5 and assigning // values at the same time int[] intArray2 = new int[5]{1, 2, 3, 4, 5};The above statement is the same as, but it assigns values to each index in {}. Example 3 : // defining array with 5 elements which // indicates the size of an array int[] intArray3 = {1, 2, 3, 4, 5};In the above statement, the value of the array is directly initialized without taking its size. So, array size will automatically be the number of values which is directly taken. Initialization of an Array after Declaration Arrays can be initialized after the declaration. It is not necessary to declare and initialize at the same time using the new keyword. However, Initializing an Array after the declaration, it must be initialized with the new keyword. It cant be initialized by only assigning values. Example :
Note : Initialization without giving size is not valid in C#. It will give a compile-time error. Example : Wrong Declaration for initializing an array
Accessing Array Elements At the time of initialization, we can assign the value. But, we can also assign the value of the array using its index randomly after the declaration and initialization. We can access an array value through indexing, placed index of the element within square brackets with the array name Implementation : Accessing Array elements using different loops
Output : For loop : 10 20 30 40 50 For-each loop : 10 20 30 40 50 while loop : 10 20 30 40 50 Do-while loop : 10 20 30 40 50One Dimensional Array In this array contains only one row for storing the values. All values of this array are stored contiguously starting from 0 to the array size. For example, declaring a single-dimensional array of 5 integers : int[] arrayint = new int[5];The above array contains the elements from arrayint[0] to arrayint[4]. Here, the new operator has to create the array and also initialize its element by their default values. Above example, all elements are initialized by zero, Because it is the int type. Example :
Output : Sun Mon Tue Wed Thu Fri SatMultidimensional Arrays The multi-dimensional array contains more than one row to store the values. It is also known as a Rectangular Array in C# because its each row length is same. It can be a 2D-array or 3D-array or more. To storing and accessing the values of the array, one required the nested loop. The multi-dimensional array declaration, initialization and accessing is as follows : // creates a two-dimensional array of // four rows and two columns. int[, ] intarray = new int[4, 2]; //creates an array of three dimensions, 4, 2, and 3 int[,, ] intarray1 = new int[4, 2, 3];Example :
Output : 2DArray[0][0] : 1 2DArray[0][1] : 2 2DArray[1][1] : 4 2DArray[2][0] 5 2DArray[1][1] (other) : 4 2DArray[1][0] (other)3 3DArray[1][0][1] : 8 3DArray[1][1][2] : 12 3DArray[0][1][1] (other): 5 3DArray[1][0][2] (other): 9 To String element one two three four five six seven eightJagged Arrays An array whose elements are arrays is known as Jagged arrays it means array of arrays. The jagged array elements may be of different dimensions and sizes. Below are the examples to show how to declare, initialize, and access the jagged arrays. Example :
Output : Element [0] Array: 1 3 5 7 9 Element [1] Array: 2 4 6 8 Another Array Element [0] Array: 1 3 5 7 9 Element [1] Array: 2 4 6 8Its possible to mix jagged and multidimensional arrays. The jagged array is an array of arrays, and therefore its elements are reference types and are initialized to null.
Output : arr[0][0, 0]:1 arr[0][0, 1]:3 arr[0][1, 0]:5 arr[0][1, 1]:7 arr[1][0, 0]:0 arr[1][0, 1]:2 arr[1][1, 0]:4 arr[1][1, 1]:6 arr[1][2, 0]:8 arr[1][2, 1]:10 arr[2][0, 0]:11 arr[2][0, 1]:22 arr[2][1, 0]:99 arr[2][1, 1]:88 arr[2][2, 0]:0 arr[2][2, 1]:9Points To Remember :
Article Tags : C# CSharp-Arrays Read Full Article |