What is a array in c#

C# - Array

We have learned that a variable can hold only one literal value, for example, int x = 1;. Only one literal value can be assigned to a variable x. Suppose, you want to store 100 different values then it will be cumbersome to create 100 different variables. To overcome this problem, C# introduced an array.
As you can see in the above figure, index is a number starting from 0, which stores the value. You can store a fixed number of values in an array. Array index will be increased by 1 sequentially till the maximum specified array size.
Array in C# is a reference type that is derived from System. Array class. Array values (elements) are stored sequentially in the memory and that's why it performs faster.

Array Declaration

An array can be declared using a type name followed by square brackets [].
Example: Array declaration in C#
  1. int[] intArray;  // can store int values
    bool[] boolArray; // can store boolean values
    string[] stringArray; // can store string values
    double[] doubleArray; // can store double values
    byte[] byteArray; // can store byte values
    Student[] customClassArray; // can store instances of Student class

Array Initialization

An array can be declared and initialized at the same time using the new keyword. The following example shows the way of initializing an array.

Example: Array Declaration & Initialization

  1. // defining array with size 5. add values later on
    int[] intArray1 = new int[5]; 
    // defining array with size 5 and adding values at the same time
    int[] intArray2 = new int[5]{1, 2, 3, 4, 5};
    // defining array with 5 elements which indicates the size of an array
    int[] intArray3 = {1, 2, 3, 4, 5};

In the above example, the first statement declares & initializes int type array that can store five int values. The size of the array is specified in square brackets. The second statement, does the same thing, but it also assignes values to each indexes in curley brackets { }. The third statement directly initializes an int array with the values without giving any size. Here, size of an array will automatically be number of values.
Initialization without giving size is NOT valid. For example, the following example would give compile-time error.

Example: Invalid Initializing an Array

  1. int[] intArray = new int[]; // compiler error: must give size of an array

Late Initialization

Arrays can be initialized after declaration. It is not necessary to declare and initialize at the same time using new keyword. Consider the following example.
Example: Late Initialization

  1. string[] strArray1, strArray2;
    strArray1 = new string[5]{ "1st Element",
                    "2nd Element", 
                    "3rd Element",
                    "4th Element",
                    "5th Element" 
    strArray2 = new string[]{ "1st Element",
                    "2nd Element",
                    "3rd Element",
                    "4th Element", 
                    "5th Element" 

However, in the case of late initialization, it must be initialized with the new keyword as above. It cannot be initialize by only assigning values to the array.
The following initialization is NOT valid:
Example: Invalid Array Initializing
  1. string[] strArray;
    strArray = {"1st Element","2nd Element","3rd Element","4th Element" };

Accessing Array Elements

As shown above, values can be assigned to an array at the time of initialization. However, value can also be assigned to individual index randomly as shown below.
Example: Setting Values
  1. int[] intArray = new int[5];
    intArray[0] = 10;
    intArray[1] = 20;
    intArray[2] = 30;
    intArray[3] = 40;
    intArray[4] = 50;

Post a Comment