Initializing the elements of an array with an array initializer. - CSharp Language Basics

CSharp examples for Language Basics:Array

Description

Initializing the elements of an array with an array initializer.

Demo Code

using System;// w ww .  j a va  2 s  .  c  o  m
class MainClass
{
   static void Main()
   {
      // initializer list specifies the value of each element
      int[] array = { 3, 2, 4, 1, 9 };
      Console.WriteLine($"{"Index"}{"Value",8}"); // headings
      // output each array element's value
      for (int counter = 0; counter < array.Length; ++counter)
      {
         Console.WriteLine($"{counter,5}{array[counter],8}");
      }
   }
}

Result


Related Tutorials