Calculating values to be placed into the elements of an array. - CSharp Language Basics

CSharp examples for Language Basics:Array

Description

Calculating values to be placed into the elements of an array.

Demo Code

using System;// w w w  .ja v a  2 s .  c  o  m
class MainClass
{
   static void Main()
   {
      const int ArrayLength = 5; // create a named constant
      int[] array = new int[ArrayLength]; // create array
      for (int counter = 0; counter < array.Length; ++counter) {
         array[counter] = 2 + 2 * counter;
      }
      Console.WriteLine($"{"Index"}{"Value",8}"); // headings
      for (int counter = 0; counter < array.Length; ++counter){
         Console.WriteLine($"{counter,5}{array[counter],8}");
      }
   }
}

Result


Related Tutorials