The Main Method – Part 3

Today I am going to finish the series of posts about the Main method, in this post we will see how to have more than one Main method in an application and how to choose among them before compiling the application.

We will start by creating a simple solution with two types, a class and a structure as you can see in Figure 1, both having a Main method.

Figure 1

Figure 1

The types implementation are below:

FirstProgram class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WordPress003
{
	class FirstProgram
	{
		static void Main(string[] args)
		{
			Console.WriteLine("Main method from FirstProgram class.");
			Console.Read();
		}
	}
}

SecondProgram structure

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WordPress003
{
	struct SecondProgram
	{
		static void Main(string[] args)
		{
			Console.WriteLine("Main method from SecondProgram struct.");
			Console.Read();
		}
	}
}

The important thing to note is that it is possible to switch between those two classes containing a Main method by means of Visual Studio or on a command switch using the command line compiler. Below are both methods demonstrated in Figure 2 and Figure 3 respectively:

Figure 2

Figure 2

In Figure 2 you can see the option Startup object which enables you to choose the desired type to use.

Figure 3

Figure 3

In Figure 3 you can realize that the same result is obtained through the /main command line switch.

Hope you enjoyed this last post about Main methods.

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.

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