How to compile with /main

BenTam-3003 686 Reputation points
2022-02-09T09:13:16.913+00:00

Dear All,

I got the following error.

CS0017  Program has more than one entry point defined. Compile with /main to specify the type that contains the entry point.

Could anybody tell me how to "compile with /main"?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,344 questions
{count} votes

4 answers

Sort by: Most helpful
  1. RLWA32 40,861 Reputation points
    2022-02-09T09:35:59.373+00:00

    For a .Net Framework project set the Startup Object in the project properties.

    Following code generates CS0017 when the Startup Object is not set -

    using System;  
    using System.Collections.Generic;  
    using System.Linq;  
    using System.Text;  
    using System.Threading.Tasks;  
      
    namespace TwoMain  
    {  
        class Program  
        {  
            static void Main(string[] args)  
            {  
                Console.WriteLine("Hello from Program.Main");  
            }  
        }  
      
        class DupMain  
        {  
            static void Main(string[] args)  
            {  
                Console.WriteLine("Hello from DupMain.Main");  
            }  
      
        }  
    }  
    

    172440-twomain.png

    Set the Startup Object in response to the error message -

    172500-twomain-set.png


  2. Karen Payne MVP 35,196 Reputation points
    2022-02-09T10:31:53.83+00:00

    Perhaps you are attempting to use Top-level statements with a version of C# before C#9, examples here. Or conventional Main.


  3. Karen Payne MVP 35,196 Reputation points
    2022-02-09T17:25:00.937+00:00

  4. Bruce (SqlWork.com) 57,481 Reputation points
    2022-02-09T17:35:01.923+00:00

    the error means when you project is built, more than one public static main was found. when the exe is built, bootstrap code is added to call this method. as there are legimate cases of having more than one Main defined, the compile has a switch to define which one the bootstrap code should use.

    in your case, it does not seem intentional. you probably have additional *.cs file in the project that define main, or you have added a reference to a dll that has a main defined.

    note: if you are using the new msbuild project files, by default all *.cs files in the folder or sub folders are included in the build. for more help, you should show us the project file