Array Declarations in C#

CsharpServer Side ProgrammingProgramming

The array is a collection of variables of the same type. They are stored contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.

Syntax

To declare an array in C# −

type[] arrayName;

Here,

  • type − is the datatype of the array in C#.
  • arrayName − The name of the array
  • [ ] − Specify the size of the array.

Example

Let us see an example to understand how to declare an array in C# −

Live Demo

using System;
namespace MyApplication {
   class MyClass {
      static void Main(string[] args) {
         // n is an array of 5 integers
         int [] a = new int[5];
         int i,j;
         /* initialize elements of array a */
         for ( i = 0; i < 5; i++ ) {
            a[ i ] = i + 10;
         }
         /* output each array element's value */
         for (j = 0; j < 5; j++ ) {
            Console.WriteLine("Element[{0}] = {1}", j, a[j]);
         }
         Console.ReadKey();
      }
   }
}

Output

Element[0] = 10
Element[1] = 11
Element[2] = 12
Element[3] = 13
Element[4] = 14
raja
Published on 24-Jul-2018 07:18:49
Advertisements