Click here to Skip to main content
13,626,436 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


55 bookmarked
Posted 22 Oct 2012
Licenced CPOL

Main Method in C#

, 24 Jan 2013
Rate this:
Please Sign up or sign in to vote.
This article is all about the Main method in C#.


When we create a Console application there is default main method created. I am always fascinated about why this main method is static void and why the arguments are supplied by default. I am going to explain this topics in this article.

Main() Method

So let's start by creating a sample console application. Open your visual studio framework. File -> New -> Project -> Select Console Application.

What is Main() Method and its purpose?

As you see a Program.cs class file create with a default Main() method. This Main() method used to signify the entry point of your application. This Main() method present in every executable C# application. C# executable means any Console application, Windows desktop application or Windows service application.

What if we have more classes with Main() method?

The class that defines the Main() method is termed as Application object. However it is possible that we have more than one class that contain Main() method. So we can set which class Main() method will be used as the entry point via the Startup Object drop down list box, located under the Application tab of the Visual Studio project properties editor.

Why Main() method is static?

Static members are scoped to the class level (rather than the object level) and can thus be invoked without the need to first create a new class instance. A main method is static because it is available to run when your program starts and as it is the entry point of the program it runs without creating an instance of the class.

In other words, static functions exist before a class is instantiated so static is applied to the main entry point (Main method).

What is string[] args in Main method? What is there use?

The string[] args may contain any number of Command line arguments which we want to pass to Main() method.

More about Main() method

By default Visual Studio defines Main() method as private. The reason behind this other application can not invoke the entry point (Main() method) of another. However please note that we can declare the Main() method as public.

Return value of Main() method?

Return value of Main() method is void (indicates no return value) by default. We can change it to one of the following possible signatures.

// int return type, array of strings as the parameter.

static int Main(string[] args)
  return 0;

static void Main()

static int Main()
  return 0;

We can return an int value in main method if we want some output when program terminates like whether the program completed execution successfully.

Also we can use string[] args parameter in Main() method if we want to process some user defined command line arguments. We can see this in more detail in below points.

What is use of return of int value in Main Method?

We can use int return value of the main method to check whether there is an error occurred in the main method. For ex if main method executes successfully then we will return 0 (this is the default value return by the main method even if it defined as void). If main method executes unsuccessfully then we will return -1.

On the Windows operating system, an application’s return value is stored within a system environment variable named %ERRORLEVEL%. An application’s return value is passed to the system at the time the application terminates, it is obviously not possible for an application to obtain and display its final error code while running.

To capture the return value we have to create a batch file. So lets start creating a main method as shown below:

static int Main(string[] args)
  Console.WriteLine("***** My First C# App *****");
  Console.WriteLine("Hello World!");

  return -1;

Now navigate to the folder where our application resides. Go to bin folder. Then go to debug folder. Create a bat file (batch file) and update its content with the following code:


@if "%ERRORLEVEL%" == "0" goto success

  echo This application has failed!
  echo return value = %ERRORLEVEL%
  goto end

  echo This application has succeeded!
  echo return value = %ERRORLEVEL%
  goto end
  echo All Done.

Note that MyConsoleApplication is nothing but application name.

Now open VS command prompt and navigate to the folder where our bat file we have just created. Execute the bat file by typing its name and press enter. You will see the output as shown below with message as This application has failed.

Change the return value as 0 in the main method as shown below.

static int Main(string[] args)
  Console.WriteLine("***** My First C# App *****");
  Console.WriteLine("Hello World!");

  return 0;

Again run the bat file from command prompt. You will get the message as “This application has succeeded” as shown in the following figure:

How to use Command line arguments in main method?

We can process the command line arguments pass to main method. One of the possible way is using for loop.

static int Main(string[] args)

  for(int i = 0; i < args.Length; i++)
  Console.WriteLine("Arg: {0}", args[i]);
  return -1;

We can pass the arguments using VS command prompt. Just go to the path where our application resides and execute the application exe by passing the arguments value as shown in following figure:

During development we can set possible command line arguments for testing purpose. To do this, right click on project and click on properties. Select Debug tab and write you command line arguments under the start options section as shown in the following figure:

So when we run the application, we will get the output as shown in the following figure.


In this article we have learned about:

  • Main method
  • How to set a particular main method as the entry point of our application?
  • How to use return value of the main method to show an error.
  • What the arguments are in main method and how to use them.


  • Pro C# 2010 .NET 4 Platform book by Andrew Troelsen.


  • Written on Oct 19 2012.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Sandesh M Patil
Team Leader
India India
Sandesh has a 8 years of total professional experience in a software development. He first handled the computer in his school days when he was in 7th std working on Lotus. In summer vacation, the school authorities allowed him and other students to use the computers for practicing Lotus and playing different games Smile | :) .

He has done computer engineering. Currently he is working in Net 4.0 framework. Even though he is using .Net 4.0 framework for development but still he has not got a chance to work with newer technologies like MVC, WCF etc. However he always try to learn these technologies from his end and eagerly waiting for a chance to work with the newer technologies like MVC, WCF and Silverlight etc.

You may also be interested in...


Comments and Discussions

QuestionYou have your Visual Studio installed differently than mine. Pin
Luke Vaxhacker7-Sep-15 10:11
memberLuke Vaxhacker7-Sep-15 10:11 
QuestionHow string[] args is optional? Pin
_Dhull 21-Jul-15 19:22
member _Dhull 21-Jul-15 19:22 
GeneralMy easiest Vote of 5 Pin
Ahmad F Hassan5-Feb-14 8:40
memberAhmad F Hassan5-Feb-14 8:40 
GeneralGood for begineer Pin
Pranay Rana3-Feb-14 3:19
memberPranay Rana3-Feb-14 3:19 
QuestionGood Article Pin
Member 1019928628-Jan-14 18:55
memberMember 1019928628-Jan-14 18:55 
AnswerRe: Good Article Pin
$andesh M Patil28-Jan-14 19:51
member$andesh M Patil28-Jan-14 19:51 
QuestionVery Clear Explanation Pin
Antariksh Verma6-Oct-13 22:52
professionalAntariksh Verma6-Oct-13 22:52 
AnswerRe: Very Clear Explanation Pin
$andesh M Patil6-Oct-13 23:19
member$andesh M Patil6-Oct-13 23:19 
QuestionI got o have bookmark this link Pin
Abhinav Gauniyal3-Oct-13 6:55
memberAbhinav Gauniyal3-Oct-13 6:55 
AnswerRe: I got o have bookmark this link Pin
$andesh M Patil3-Oct-13 7:13
member$andesh M Patil3-Oct-13 7:13 
GeneralMy vote of 3 Pin
thefiloe4-May-13 23:34
memberthefiloe4-May-13 23:34 
GeneralRe: My vote of 3 Pin
$andesh M Patil5-May-13 0:40
member$andesh M Patil5-May-13 0:40 
GeneralMy vote of 5 Pin
Minghang31-Jan-13 3:31
memberMinghang31-Jan-13 3:31 
GeneralRe: My vote of 5 Pin
SChristmas3-Mar-13 22:46
memberSChristmas3-Mar-13 22:46 
GeneralMessage Closed Pin
11-Nov-12 21:37
memberBjörn Friedrich11-Nov-12 21:37 
GeneralRe: My vote of 1 Pin
SChristmas13-Nov-12 22:24
memberSChristmas13-Nov-12 22:24 
AnswerRe: My vote of 1 Pin
Björn Friedrich20-Nov-12 2:50
memberBjörn Friedrich20-Nov-12 2:50 
GeneralRe: My vote of 1 Pin
Forogar17-Dec-12 5:12
memberForogar17-Dec-12 5:12 
GeneralRe: My vote of 1 Pin
SChristmas17-Dec-12 6:05
memberSChristmas17-Dec-12 6:05 
SuggestionRe: My vote of 1 Pin
Paw Jershauge17-Dec-12 11:45
memberPaw Jershauge17-Dec-12 11:45 
GeneralRe: My vote of 1 Pin
Björn Friedrich4-Jan-13 2:38
memberBjörn Friedrich4-Jan-13 2:38 
AnswerRe: My vote of 1 Pin
Paw Jershauge4-Jan-13 8:23
memberPaw Jershauge4-Jan-13 8:23 
GeneralRe: My vote of 1 Pin
Björn Friedrich5-Jan-13 4:52
memberBjörn Friedrich5-Jan-13 4:52 
SuggestionTidier sample output Pin
CHill6031-Oct-12 1:26
memberCHill6031-Oct-12 1:26 
GeneralRe: Tidier sample output Pin
SChristmas31-Oct-12 1:35
memberSChristmas31-Oct-12 1:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.180712.1 | Last Updated 24 Jan 2013
Article Copyright 2012 by Sandesh M Patil
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid