Dark Mode
Image

C# Properties

C# Inheritance

C# Polymorphism

C# Strings

C# Generics

C# Delegates

C# Reflection

Anonymous Function

C# Multithreading

C# Synchronization

C# Web Service

C# Misc

C# New Features

C# Programs

ADO.NET Tutorial

ASP.NET Tutorial

C# Arrays

C# Arrays

Like other programming languages, array in C# is a group of similar types of elements that have contiguous memory location. In C#, array is an object of base type System.Array. In C#, array index starts from 0. We can store only fixed set of elements in C# array.

C# array

Advantages of C# Array

  • Code Optimization (less code)
  • Random Access
  • Easy to traverse data
  • Easy to manipulate data
  • Easy to sort data etc.

Disadvantages of C# Array

  • Fixed size

C# Array Types

There are 3 types of arrays in C# programming:

  1. Single Dimensional Array
  2. Multidimensional Array
  3. Jagged Array

C# Single Dimensional Array

To create single dimensional array, you need to use square brackets [] after the type.

 int[] arr = new int[5];//creating array  

You cannot place square brackets after the identifier.

int arr[] = new int[5];//compile time error  

Let's see a simple example of C# array, where we are going to declare, initialize and traverse array.

 

using System; 

public class ArrayExample 

    public static void Main(string[] args) 

    { 

        int[] arr = new int[5];//creating array 

        arr[0] = 10;//initializing array 

        arr[2] = 20; 

        arr[4] = 30; 

 

        //traversing array 

        for (int i = 0; i < arr.Length; i++) 

        { 

            Console.WriteLine(arr[i]); 

        } 

    } 

 

Output:

10

0

20

0

30

Comment / Reply From