Thursday, April 28, 2011

C# Compiler not recognizing overloaded Main

Program "" has more than one entry point defined: 'Class.Main()'. Compile with /main to specify the type that contains the entry point.

I have searched and searched, and have only found the syntax to specify the class of the entry point, (/main:class) but not the type. Can anyone help?

static void Main()
{
}

static void Main(string[] args)
{
}
From stackoverflow
  • I don't believe it's possible to overload main, for that exact reason: there can be only one entry point into your program!

    What the "/main" allows you to do is specify the type (i.e. the class or struct) that contains the main entry point, and not the signature (i.e. which of the overloading) so the compiler is left with ambiguity.

    Jon Skeet : It's overloading, not overriding. And a type isn't the same as an assembly.
    Miky Dinescu : I stand corrected..
  • You can't do this, basically.

    You can only specify that a type is an entry point, not which Main overload within a type should be the entry point.

    You could create a nested class containing one of them, if you want to keep the code within the same outer type:

    using System;
    using System.IO;
    using System.Text.RegularExpressions;
    
    class Test
    {
        class Parameterless
        {
            static void Main()
            {
            }
        }
    
        static void Main(string[] args){}
    }
    

    You then need to use either /main:Test or /main:Test.Parameterless depending on which one you want to call, or use the Application Entry Point in the project properties in Visual Studio.

  • As a last resort, you could try

    static void Main(string[] args)
    {
        if (args == null)
        {
    
        }
        else
        {
    
        }
    }
    

    Might not be the best method, but it would work. It is against the concepts of an application to have 2 entry points.

    Hawker : args will never be null, it will just be a empty string[]. if(args.Length == 0)
    Jon Skeet : (Unless it's called directly from other code, of course.)

0 comments:

Post a Comment