How to: Access Command-Line Arguments Using foreach (C# Programming Guide)


Updated: July 20, 2015

System_CAPS_ICON_note.jpg Note

For the latest documentation on C#, visit the C# Guide on

Another approach to iterating over the array is to use the foreach statement as shown in this example. The foreach statement can be used to iterate over an array, a .NET Framework collection class, or any class or struct that implements the IEnumerable interface.

System_CAPS_ICON_note.jpg Note

When running an application in Visual Studio, you can specify command-line arguments in the Debug Page, Project Designer.

This example demonstrates how to print out the command line arguments using foreach.

// arguments: John Paul Mary

    class CommandLine2
        static void Main(string[] args)
            System.Console.WriteLine("Number of command line parameters = {0}", args.Length);

            foreach (string s in args)
    /* Output:
        Number of command line parameters = 3

Command-line Building With csc.exe
C# Programming Guide
foreach, in
Main() and Command-Line Arguments
How to: Display Command Line Arguments
Main() Return Values