articles

Home / DeveloperSection / Articles / Array in C#

Array in C#

Sumit Kesarwani 3825 10-May-2013

In this article, I’m explaining the concept of array in c# with single dimension and multi dimension.

An array stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables. A specific element in an array is accessed by an index. 

In C#, an array index starts at zero. That means the first item of an array starts at the 0thposition. The position of the last item on an array will total number of items - 1. So if an array has 10 items, the last 10th item is at 9th position. 

In C#, arrays can be declared as fixed length or dynamic.

A fixed length array can store a predefined number of items.

dynamic array does not have a predefined size. The size of a dynamic array increases as you add new items to the array. You can declare an array of fixed length or dynamic. You can even change a dynamic array to static after it is defined.

Declaring Arrays

To declare an array in C#, you can use the following syntax:

datatype[]  arrayName;

where

  • datatype is used to specify the type of elements to be stored in the array.
  • [ ] specifies the rank of the array. The rank specifies the size of the array.
  • arrayName specifies the name of the array.
Initializing an Array

Declaring an array does not initialize the array in the memory. When the array variable is initialized, you can assign values to the array.

Array is a reference type, so you need to use the new keyword to create an instance of the array.

For example,

double[] balance = new double[10];

Example
using System;

 
namespace ArrayConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter maximum value for array=");
            int n=Convert.ToInt32(Console.ReadLine());
 
            int[] arr=newint[n];
            int temp;
            Console.WriteLine("Enter value in Array");
            for (inti=0; i<=n-1; i++)
            {
                Console.Write("Enter Element "+i+":");
                arr[i] =Convert.ToInt32(Console.ReadLine());
            }
            Console.WriteLine();
            Console.WriteLine("Data in Array Before Sorting");
            for (inti=0; i<=n-1; i++)
            {
                Console.Write(arr[i]+"\t");
            }
 
            for (inti=0; i<=n-1; i++)
            {
                for (intj=0; j<=i; j++)
                {
                    if (arr[i] <arr[j])
                    {
                        temp=arr[i];
                        arr[i] =arr[j];
                        arr[j] =temp;
                    }
                }
            }
            Console.WriteLine("\n");
            Console.WriteLine("Data in Array After Sorting");
            for (inti=0; i<=n-1; i++)
            {
                Console.Write(arr[i]+"\t");
            }
            Console.WriteLine();
        }
    }
}

 Output                                             

Array in C#

Example-2: Multidimensional Array
using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Array2DConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            int[,] arr=newint[3, 3];
            Console.WriteLine("Enter value in 2D Array");
            for (inti=0; i<3; i++)
            {
                for (intj=0; j<3; j++)
                {
                    Console.Write("Enter Value =");
                    arr[i, j] =Convert.ToInt32(Console.ReadLine());
                }
            }
            Console.WriteLine("\nData in 2D Array");
            for (inti=0; i<3; i++)
            {
                for (intj=0; j<3; j++)
                {
                    Console.WriteLine("arr["+i+"]"+"["+j+"] = "+arr[i,j]);
                }
            }
        }
    }
}

 Output

Array in C#

 


c# c# 
Updated 07-Sep-2019

Leave Comment

Comments

Liked By