C# Arrays

  • An array is a collection of data of the same type stored in a contiguous memory location.
  • The array is the derived data type in the C programming language which can store the primitive data types, such as pointers structure, etc.

Advantages:

1) Code Optimization: Less code to access the data.

2) Ease of traversing: By using the for loop, we can retrieve the elements of an array easily.

3) Ease of sorting: To sort the elements of the array, we need a few lines of code only.

4) Random Access: We can access any element randomly using the array.

Disadvantages:

1) Fixed Size: Whatever size, we define at the time of declaration of the array, we can’t exceed the limit. So, it doesn’t grow the size dynamically like Linked List which we will learn later.

Properties of Arrays:

  • Each element of an array is of the same data type and carries the same size.
  • Elements of an array are stored in contiguous memory locations where the first element is stored at the smallest memory location
  • Elements of the array can be randomly accessed since we can calculate the address of each element of the array with the given base address and the size of the data element.

Array Declaration:

syntax:

data_type[] array_name = new data_type[size];
 Example:
int[] arr = new int[5];
Initializing Array:
We can initialize an array during the declaration:
int[] arr =  new int[] {1,2,3,4,5};

we can also initialize an array like this:

int[] arr = {1, 2, 3, 4, 5};
Here, we haven’t specified the size however the compiler knows its size is 5 as we are initializing it with 5 elements.

Initializing Example:
 
 Accessing elements of the array:
 
  • once the array is  declared, then the individual elements in the array can be referred to by the number in the brackets following the array name this number specifies the position of the element in the array 
  • Subscript starts with 0, which means arr[0] represents the first element in the array arr. 
  • Thus num[2] is not the second element but this is considered as the third element of the array.

For example:

int mydata[20]; 
mydata[0] /* first element of array mydata*/ 
mydata[19] /* last (20th) element of array mydata*/
Types of arrays:
1. One-dimensional array
1. Two-dimensional array
 

One-dimensional array:

One dimensional array is an array that has only one subscript specification that is needed to specify a particular element of an array. A one-dimensional array is a structured collection of components (often called array elements) that can be accessed individually by specifying the position of a component with a single index value. 

Syntax:

data_type[] array_name = new data_type[size];

Entering data into an array

for(i=0;i<=4;i++)
{
    Console.WriteLine("Enter data: ");
    // Entering data into an array named arr.
    arr[i] = Convert.ToInt32(Console.ReadLine());
}

Here we are iterating the array from 0 to 4 because the size of the array is 5, and all the values will be stored in the corresponding array elements using scanf function. 

Reading data from an Array

Suppose we have to display the above entered elements

for(i=0;i<4;i++)
{
    Console.WriteLine("Element at {0} is {1}", i, arr[i]);
}
// Program to sum the elements of an array.
using System;
  
namespace LearningProgram
{   
    class LearnProgram
    {   
        // Main function
        static void Main(string[] args)
        {
            int n = 0, sum = 0, i = 0;
            Console.WriteLine("Enter Array size: ");
            n = Convert.ToInt32(Console.ReadLine());
            // declaring array.
            int[] arr = new arr[n];
            
            for(i=0; i<n; i++){
                Console.WriteLine("Enter Data: ");
                arr[i] = Convert.ToInt32(Console.ReadLine());
            }
            for(i=0; i<n; i++){
                sum = sum+arr[i];
            }
            Console.WriteLine("Sum of Elements of array is, "+sum);
        }
    }
}