0

I have written some sample code in C# to check a few concepts where I have declared two main methods; please find it below for your reference:

class Program
{
    public static void Main()
    {
        Console.WriteLine("Main method called");
        Console.ReadKey();
    }

    public static void Main(char[] args)
    {
        foreach (char c in args)
        {
            Console.WriteLine(c.ToString());
        }
        Console.WriteLine("Char main method called");
        Console.ReadKey();
    }
}

The code compiles successfully with one warning message:

Program.Main(char[]) has the wrong signature as the entry point

As the CLR has defined Main(string[]) or Main() as the starting point, is there a possibility that I can make public static void Main(char[] args) as the starting point?

I have found multiple links on Stack Overflow, but none of them were much useful to me, such as C# Entry Point Function.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nishant Kumar
  • 463
  • 2
  • 9
  • 18
  • I know it would be out of the box question but if it is not supported then why even C# is allowed to declare multiple main method with different signature in the same class file. – Nishant Kumar Jan 14 '20 at 05:28
  • Either of those two signatures is allowed, but you can't have both in the same class and have one of them be your real Main method. If you have two classes that implement Main with one of the valid signatures, then you can pick which *class* has the one to use. Don't forget that you can pull the command line arguments out of the Environment class, so you can just have one method and decide what do to in you Main method logic. What did you expect to happen with two Main methods? – Flydog57 Jan 14 '20 at 05:36
  • @Flydog57, i was checking the concept that if two main methods can be declared in same class file which is not useful then why to even allowed to declare it.. this question was asked to me and i was not sure about it; so was checking the concept. – Nishant Kumar Jan 14 '20 at 06:17

1 Answers1

3

You can only have one entry (Main) point. Your Main method's signature should match either with Main() or Main(string[]) (actually there are more valid signatures. You can check them by going by the link at the bottom), so you can have only one of them in your code. If the signature is different from Main() or Main(string[]), then it is not considered as an entry point.

For example, you can have:

// Will compile. Warning CS0028  'Program.Main(int)' has the wrong signature to be an entry point.

static void Main() // Entry point
{
}

static void Main(int n) // Not an entry point
{
}

But you can't use:

// Compiler Error CS0017 Program has more than one entry point defined.

static void Main() // Entry point
{
}

static void Main(string[] args) // Another entry point
{
}

For more information, please check Main() and command-line arguments (C# Programming Guide).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
SᴇM
  • 7,024
  • 3
  • 24
  • 41
  • 1
    As a point of order: MainAsync is also possible: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-7.1/async-main – Frank R. Haugen Jan 14 '20 at 05:45