Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2005
Visual Studio
Visual C#

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
C# Programming Guide
Main() and Command Line Arguments (C# Programming Guide)

The Main method is the entry point of your program, where you create objects and invoke other methods. There can only be one entry point in a C# program.

C#
class TestClass
{
    static void Main(string[] args)
    {
        // Display the number of command line arguments:
        System.Console.WriteLine(args.Length);
    }
}
  • The Main method is the entry point of your program, where the program control starts and ends.

  • It is declared inside a class or struct. It must be static and it should not be public. (In the example above it receives the default access of private.)

  • It can either have a void or int return type.

  • The Main method can be declared with or without parameters.

  • Parameters can be read as zero-indexed command line arguments.

  • Unlike C and C++, the name of the program is not treated as the first command line argument.

For more information, see the following sections in the C# Language Specification:

  • 1.1 Hello World

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Accessing command line arguments outside of Main      Daniel Rieck   |   Edit   |   Show History

If you need to access command line arguments somewhere else than the Main method, you can call the Environment.GetCommandLineArgs() method.

Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker