The Main Method – Part 1

Today I am going to talk about something simple, the Main method, but even the simplest thing has its particularities. The Main method of a program is considered by convention its entry point (C# Language Specification Version 3.0, September 2007, Section 1.1) and this entry point must be declared with the static modifier and it is case sensitive like all methods and C# language itself.

Example:

using System;

namespace WordPress001
{
	class Program
	{
		static void Main(string[] args)
		{
			Console.WriteLine("Just another hello world!");
		}
	}
}

So far, so good but is it possible to have a program without a Main method? The answer is yes, by compiling a program without a Main method we have a library to be used by other programs and a program that has at least one Main method is called an application (C# Language Specification Version 3.0, September 2007, Section 1.2).

The Main method can have the following definitions:

static void Main() {...}
static void Main(string[] args) {...}
static int Main() {...}
static int Main(string[] args) {...}

As you could see some definitions allow your program to read values as arguments and to return values, the parameter can be passed through a command prompt, by a shortcut or by other applications.

Example:

using System;

namespace WordPress001
{
	struct Program
	{
		static int Main(string[] arguments)
		{
			if (arguments.Length > 0)
			{
				foreach (var argument in arguments)
				{
					Console.WriteLine(argument);
				}
				return 0;
			}
			return 1;
		}

		static void Main(object o)
		{
			Console.WriteLine(o);
		}
	}
}

The application above reads the array arguments and prints every element of it, the arguments string array is never null but may have a length of 0 if no arguments is passed to the application. If at least one argument was passed to the application it returns 0 (generally used indicating success) otherwise it returns 1 (a code different than 0 may indicate a problem). This kind of application is generally used when developing console applications, you could write an application to copy a file and return a code of 0 indicating success or 1 indicating failure and call it from another application. This concept of writing one application and call it from another is very used on unix like systems and is known as front-ent and back-end applications.

Note that at line 5 I used a struct instead of a class and it is totally acceptable. Note as well that the Main method appears more than once, this was intentional as one class or struct may have more than one Main method, but only one can be eligible as the application entry point, that is, have one of the four definitions mentioned above.

Hope you enjoyed the post, this is the first one of a serie about the Main method.

Many more to come.

See you.

Advertisements

About xcap2000

I'm just a developer who's interested in C# and Microsoft Technologies.
This entry was posted in C#. Bookmark the permalink.

One Response to The Main Method – Part 1

  1. Pingback: The Main Method – Part 2 « Playing with C#

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s