Arrays in C#

The array is a data structure that is used to store the homogeneous data inside. We can store any data that is a primitive type or any Object type inside an array but the data should be of the same type and it should be in the specified array size.

How to create an array :

type[] variable-name = new type[size];
// example
int[] num = new num[5];
  • This is the basic syntax of array creation.
    • type - Type is nothing but datatype or object type of the array you want to create.
    • variable-name - This is a variable name that can be used to access the elements of an array.
    • new - This is a keyword of C# which is used for object creation.
    • size - The size is used to create a specific length array element.

Note: Array is an object and every array has a fixed size and the size of the array can't be changed after creation.

Creating an array using different techniques :

The array can be created in multiple ways they are :

Way 1:

The array can be created in 3 steps array declaration, creation, and initialization.

i. Array declaration :

type variable_name[];
//example
int num[];
  • The above line of code will just declare the int array with the name num.

ii. Array creation :

variable_name = new type[size];
//example
num = new int[3];
  • The above line of code will create the array with a given size that is 3. This array num will have the size of 3. and all the elements of the array are default values with respect to the datatypes here in this case datatype is int so the default value is 0.

iii. Array initialization :

variable_name[index];
//example
num[0] = 11;
num[1] = 22;
num[2] = 33;

Note: The array index starts from zero

  • As we have initialized the array with size 3. We can initialize the array element using an index.

  • For the above example with size 3 index values will be 0, 1, '2' and the values assigned are '11`, '22', '33' respectively.

Way 2 :

As we have seen above it requires 3 steps to create an array but we can do it in a different way as shown below.

type[] variable_name = {e1,e2,..en};
//or
type[] variable_name = new type[]{e1,e2,..en};
//both of the above line does the same thing
  • In the above syntax as you see we can declare and initialize the values of the array without mentioning the size. We can use this method whenever we know the values we are going to store.

Note: We can use any technique to create the array depending on the requirements.

Accessing the array elements :

  • We can retrieve/access the array elements using the loops. We use the for, while, and foreach loop to access the array elements.

  • We are going to learn about retrieving the array element using the for loop.

using System;
class MainClass {
  public static void Main() 
  {
    //Array declaration and initialization
    int[] num = {1,2,3}; 

    for(int i=0;i<num.Length;i++)
    {
      Console.Write(num[i]+" ");
    }
  }
}

Note: We can use the Length property of the array to find the unknown array length.

  • The above for loop will create the index values using the given condition. The index values generated by the for loop for the above-given condition of the index are 0, 1, and 2.
  • The above code inside the loop is the same as adding the array element to the respective index as we have seen above. Instead of adding we can retrieve the element using an index same as adding them to the respective index.

Changing the array element using the index :

We can update the array elements by retrieving them using the index and reassigning them with the new values.

num[0] = 44;

Note: If you try to store the string values inside the int array it will throw an exception that is Cannot implicitly convert type 'string' to 'int'.