Using command-line arguments to initialize an array. : Main « Language Basics « C# / C Sharp






Using command-line arguments to initialize an array.


using System;

public class InitArray
{
   public static void Main( string[] args )
   {
         int arrayLength = Convert.ToInt32( args[ 0 ] );
         int[] array = new int[ arrayLength ]; // create array

         int initialValue = Convert.ToInt32( args[ 1 ] );
         int increment = Convert.ToInt32( args[ 2 ] );

         for ( int counter = 0; counter < array.Length; counter++ )
            array[ counter ] = initialValue + increment * counter;

         Console.WriteLine( "{0}{1,8}", "Index", "Value" );
         for ( int counter = 0; counter < array.Length; counter++ )
            Console.WriteLine( "{0,5}{1,8}", counter, array[ counter ] );
   }
}
  
       








Related examples in the same category

1.Variations on the Main() Method
2.This is a simple C# programThis is a simple C# program
3.Static Main functionStatic Main function
4.Every console app starts with MainEvery console app starts with Main
5.Main Function: Multiple Mains
6.The Main Function:Command-Line Parameters
7.The Main Function:Returning an Int StatusThe Main Function:Returning an Int Status
8.The Main FunctionThe Main Function
9.Return different value to the operating system based on the argument length
10.Deal with the arguments