Pages

Sunday, March 27, 2011

Example of multiple Main() methods in C#.Net:


C# Syntax:


using System;
namespace m1
{

    class c1
    {
        public static void Main()
        {
            Console.WriteLine("this is from the class 1 of the namespace 1");
            Console.ReadLine();

        }
    }
    class c2
    {
        public static void Main()
        {
            Console.WriteLine("this is from the class 2 of the namespace 2");
            Console.ReadLine();

        }
    }

}



How to run in console:


C:\>csc manyMain.cs
Microsoft (R) Visual C# 2008 Compiler version 3.5.21022.8
for Microsoft (R) .NET Framework version 3.5
Copyright (C) Microsoft Corporation. All rights reserved.

manyMain.cs(16,20): error CS0017: Program 'c:\manyMain.exe' has more than one
        entry point defined: 'm1.c1.c2.Main()'.  Compile with /main to specify
        the type that contains the entry point.
manyMain.cs(7,20): error CS0017: Program 'c:\manyMain.exe' has more than one
        entry point defined: 'm1.c1.Main()'.  Compile with /main to specify the
        type that contains the entry point.

C:\>csc manyMain.cs /main:m1.c1
Microsoft (R) Visual C# 2008 Compiler version 3.5.21022.8
for Microsoft (R) .NET Framework version 3.5
Copyright (C) Microsoft Corporation. All rights reserved.


C:\>manyMain
this is from the class 1 of the namespace 1



4 comments: