CSharp - Array Array initialization

Introduction

An array initialization expression declares and populate an array in a single step:

char[] vowels = new char[] {'a','e','i','o','u'};

or simply:

char[] vowels = {'a','e','i','o','u'};

using System;
class MainClass
{
   public static void Main(string[] args)
   {
       char[] vowels = {'a','e','i','o','u'};
       
       for (int i = 0; i < vowels.Length; i++){
           Console.Write (vowels[i]);            // aeiou       
       }
   }
}

Consider the following example.

Here we have created and printed an array of integers.

This array is containing three integers.

Demo

using System;

class Program/*  www . j  a v  a 2 s. c  o m*/
{
    static void Main(string[] args)
    {
        int[] myInts = new int[3];
        myInts[0] = 5;
        myInts[1] = 15;
        myInts[2] = 25;
        Console.WriteLine("Elements of myInts array are as follows:");
        for (int i = 0; i < 3; i++)
        {
            Console.WriteLine(myInts[i]);
        }
    }
}

Result