CS Array

The collection or series of elements will be stored in an array. These are the same type of elements. Thus the array might be a set of such values as [ 1, 2, 3, 4,5 ] if you have an array of integer values. In this set, the number of elements in the array is 5.

Arrays are helpful if a set of values of the same type is to be stored. Therefore, we can only declare one variable instead of declaring a variable for every element. This variable indicates an array or list of elements, which will be responsible for storing the array elements.

 

 

In otherwords,

Like other programming languages, in C # the array is a group of similar element types that have contiguous memory locations. In C#, array is an object of base type System.Array. In C #, the index array begins with 0. Only fixed set of elements can be stored in the C # array.

Advantages of C# Array

  • Optimization of the code (less code)
  • Random Access of elements
  • Easy transmission of data
  • Data can be easily handled
  • Sort data easily, etc.

 

Disadvantages of C# Array

  • Array sized is fixed

 

In C # programming there are 3 kinds of Arrays:

  • Single Dimensional Array
  • Multidimensional Array
  • Jagged Array

 

You can use the following syntax to declare an array in C#


datatype[] arrayName;

where:

  • Datatatype is used for the type of element in the array.
  • The array rank is specified by [ ]. The rank determines the array size.
  • arrayName sets the array name.

 

For Example:


double[] Price;

The declaration of an array does not initialize the array in the array. We can allocate values to the array if the array variable has been initialized. Array is a reference type, so to create a  instance of the array you must use the new keyword.

For Example:


double[] Price = new double[10];

By using the index number, we can assign values to each array element.

For Example:


double[] Price = new Price[10];
balance[0] = 500.00;

At the time of declaration, we may assign values to the array as shown.


double[] Price = { 234.00, 4523.69, 3421.00};

As shown, we can also create and initialize an array.


int [] marks = new int[5]  { 99,  98, 92, 97, 95};

As shown, we can also skip the size of the array


int [] marks = new int[]  { 99,  98, 92, 97, 95};

A variable array can be copied into another target array variable. In such case, the source and the target point to the same memory location.


int [] marks = new int[]  { 99,  98, 92, 97, 95};
int[] score = marks;

When creating an array, each array element is implicitely triggered to a default value according to the array type. For example, All elements are initialized for an int array to 0s.

 

 

An element is accessed by indexing the name of the array. This is performed by inserting the index of element within the square brackets after the array name.

For Example:


int subject = marks[3];

The example below shows the concepts declaration, assignment and access arrays mentioned above.



using System;
					
public class Program
{
	public static void Main()
	{
		int []  n = new int[10]; /* n is an array of 10 integers */
         int i,j;

         /* initialize elements of array n */
         for ( i = 0; i < 10; i++ ) {
            n[ i ] = i + 100;
         }
         
         /* output each array element's value */
         for (j = 0; j < 10; j++ ) {
            Console.WriteLine("Element[{0}] = {1}", j, n[j]);
         }
         
	}
}

Output

Element[0] = 100
Element[1] = 101
Element[2] = 102
Element[3] = 103
Element[4] = 104
Element[5] = 105
Element[6] = 106
Element[7] = 107
Element[8] = 108
Element[9] = 109

 

In the previous example, for accessing each array element, we used a for loop. We can also use a for each statement in an array.


using System;
					
public class Program
{
	public static void Main()
	{
		int []  n = new int[10]; /* n is an array of 10 integers */
         
         /* initialize elements of array n */
         for ( int i = 0; i < 10; i++ ) {
            n[i] = i + 100;
         }
         
         /* output each array element's value */
         foreach (int j in n ) {
            int i = j-100;
            Console.WriteLine("Element[{0}] = {1}", i, j);
         }
         
	}
}

Output:

Element[0] = 100
Element[1] = 101
Element[2] = 102
Element[3] = 103
Element[4] = 104
Element[5] = 105
Element[6] = 106
Element[7] = 107
Element[8] = 108
Element[9] = 109