Until now, we have used variable names to denote memory locations in the system, which are used to store individual data for a program. But there may be some situations where you may need to store a series of similar type data under a common name to increase simplicity.
This can be done using arrays. In this chapter, you will learn about the concept of arrays in C#.



What Are Arrays in C#

In C #, an array can be defined as a collection of variables of the same type considered under the same name and deployed in a contiguous memory location. Here each data item is termed as an element of an array. It requires a data type to determine what type of value the entire array will store. These data types can be primary data types like char, int, or float.

Characteristics of C# Arrays

  • Since arrays are C# objects, finding their length is possible using member length, which is done using sizeof in C and C++.
  • In C#, arrays get allocated dynamically.
  • Arrays in C# are declared as that of other variables along with a [] after the data type.
  • Array elements are ordered, where each of them has an index starting from 0.
  • Elements of an array can be of any type, which includes array type also.
  • Arrays in C# are objects of the base type, i.e., System.Array.

Types of Arrays in C#

C# provides three different types of arrays. These are:

  • Single Dimensional Array: A single pair of the square bracket is used to represent a single row (hence 1-D) of values under a single name.
    Creating a 1-D Array, int[] ar = new int[6];
  • Multidimensional Array: is also called rectangular arrays, and they can be 2D, 3D, or multi-D arrays, and it can be visualized in row-column format, i.e., matrix format.
    Creating a Multi-dimensional array, int[,] ar = new int[2, 4]; or int[,,] ar = new int[2, 3, 4];
  • Jagged Array: These types of arrays are mainly called "array of arrays". The element size differs in the case of jagged arrays.
    Creating a Jagged array, int[][] ar = new int[3][];

Program for Single Dimensional Array in C#

Example:

using System;  
public class ArrayProg  
{  
    public static void Main(string[] args)  
    {  
        int[] arColl = { 20, 30, 40, 50, 60 }; 
        for (int g = 0; g < arColl.Length; g++)  
        {  
            Console.WriteLine( arColl[g] );  
        }  
    }  
}  

Program Output:

20
30
40
50
60

Program for Multi-Dimensional Array in C#

Example:

using System;  
public class Multi_ArrayProg 
{  
    public static void Main(string[] args)  
    {  
        int[,] arColl = new int[3, 3]; 
        arColl[0,1] = 20; 
        arColl[1,2] = 30;  
        arColl[2,0] = 40; 
        for(int g=0; g<3; g++){  
            for(int h=0; h < 3; h++) {  
                Console.Write( arColl[g, h] + " " );  
            }  
            Console.WriteLine(); 
        }  
    }  
}

Program Output:

0 20 0 
0 0 30 
40 0 0

Program for Jagged Array in C#

Example:

public class JaggedArrayProg  
{  
    public static void Main()  
    {  
        int[][] arColl = new int[2][]; 
        arColl[0] = new int[] { 62, 24, 9, 37 }; 
        arColl[1] = new int[] { 43, 42, 68, 80, 15, 73 };    
        for (int i = 0; i < arColl.Length; i++)  
        {  
            for (int k = 0; k < arColl[i].Length; k++)  
            {  
                System.Console.Write( arColl[i][k] + " " );  
            }  
            System.Console.WriteLine();  
        }  
    }  
}

Program Output:

62 24 9 37 
43 42 68 80 15 73


Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram