YouTip LogoYouTip

Csharp Array

An array is a fixed-size, ordered collection of elements of the same type. An array is a collection used to store data, and is generally considered a collection of variables of the same type. Declaring an array variable does not declare individual variables like number0, number1, ..., number99, but rather declares a single variable like numbers, and then uses numbers, numbers, ..., numbers to represent the individual variables. A specific element in the array is accessed via its index. All arrays are composed of contiguous memory locations. The lowest address corresponds to the first element, and the highest address corresponds to the last element. ![Image 1: Arrays in C#](#) To declare an array in C#, you can use the following syntax: datatype[] arrayName; Where, * _datatype_ specifies the type of elements stored in the array. * __ specifies the rank (dimension) of the array. The rank specifies the size of the array. * _arrayName_ specifies the name of the array. For example: double[] balance; Declaring an array does not initialize the array in memory. When you initialize the array variable, you can assign a value to the array. An array is a reference type, so you need to use the **new** keyword to create an instance of the array. For example: double[] balance = new double; You can assign a value to a single array element using its index number, like this: double[] balance = new double; balance = 4500.0; You can assign values to an array when declaring it, like this: double[] balance = { 2340.0, 4523.69, 3421.0}; You can also create and initialize an array, like this: int [] marks = new int { 99, 98, 92, 97, 95}; In the above case, you can also omit the size of the array, like this: int [] marks = new int[] { 99, 98, 92, 97, 95}; You can also assign an array variable to another target array variable. In this case, both the target and source will point to the same memory location: int [] marks = new int[] { 99, 98, 92, 97, 95};int[] score = marks; When you create an array, the C# compiler implicitly initializes each array element to a default value based on the array type. For example, all elements of an int array are initialized to 0. Elements are accessed using the array name followed by the index in square brackets. For example: double salary = balance; Here is an example using the three concepts mentioned above: declaring, assigning, and accessing an array: ## Example using System; namespace ArrayApplication { class MyArray { static void Main(string[] args) { int[]n =new int;/* n is an array of 10 integers */ int i,j; /* Initialize elements of array n */ for( i =0; i <10; i++) { n= i +100; } /* Output each array element's value */ for(j =0; j <10; j++) { Console.WriteLine("Element[{0}] = {1}", j, n); } Console.ReadKey(); } } } When the above code is compiled and executed, it produces the following result: Element = 100Element = 101Element = 102Element = 103Element = 104Element = 105Element = 106Element = 107Element = 108Element = 109 In the previous example, we used a for loop to access each array element. You can also use a **foreach** statement to traverse the array. The following example uses foreach to traverse an array: ## Example using System; namespace ArrayApplication { class MyArray { static void Main(string[] args) { int[]n =new int;/* n is an array of 10 integers */ /* Initialize elements of array n */ for(int i =0; i <10; i++) { n= i +100; } /* Output each array element's value */ foreach(int j in n ) { int i = j-100; Console.WriteLine("Element[{0}] = {1}", i, j); } Console.ReadKey(); } } } When the above code is compiled and executed, it produces the following result: Element = 100Element = 101Element = 102Element = 103Element = 104Element = 105Element = 106Element = 107Element = 108Element = 109 Arrays are very important in C#, and more details need to be understood. The following lists some important array-related concepts that C# programmers must be clear about: | Concept | Description | | --- | --- | | (#) | C# supports multidimensional arrays. The simplest form of a multidimensional array is the two-dimensional array. | | (#) | C# supports jagged arrays, which are arrays of arrays. | | (#) | You can pass a pointer to an array to a function by specifying the array name without an index. | | (#) | This is typically used to pass a variable number of parameters to a function. | | (#) | Defined in the System namespace, it is the base class for all arrays and provides various properties and methods for arrays. |
← Csharp Multi Dimensional ArrayCsharp Nullable β†’