Troubleshooting "program does not contain a static 'Main' method" when it clearly does...?

C#Compilation

C# Problem Overview


My MS Visual C# program was compiling and running just fine. I close MS Visual C# to go off and do other things in life.

I reopen it and (before doing anything else) go to "Publish" my program and get the following error message:

> Program C:\myprogram.exe does not contain a static 'Main' method suitable for an entry point

Huh? Yes it does... and it was all working 15 min earlier. Sure, I can believe that I accidentally hit something or done something before I closed it up... but what? How do I troubleshoot this?

My Program.cs file looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Threading;

namespace SimpleAIMLEditor
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            Application.Run(new mainSAEForm());
        }
    }
}

...and there are some comments in there. There are no other errors.

Help?

C# Solutions


Solution 1 - C#

Are the properties on the file set to Compile?

Solution 2 - C#

I was struggle with this error just because one of my class library projects was set acceddentaly to be an console application

so make sure your class library projects is class library in Output type

enter image description here

Solution 3 - C#

Oke, I was looking at this issue as well. And in my case the solutions was too easy. I added a new empty project to the solution. The newly added project is automatically set as a console application. But since the project added was a 'empty' project, no Program.cs existed in that new project. (As expected)

All I needed to do was change the output type of the project properties to Class library

Solution 4 - C#

If you have a WPF or Silverlight application, make sure that App.xaml has "ApplicationDefinition" as the BuildAction on the File Properties.

Solution 5 - C#

I had this error and solved by this solution.

--> Right click on the project

--> and select "Properties"

--> then set "Output Type" to "Class Library".

Solution 6 - C#

Check your project's properties. On the "Application" tab, select your Program class as the Startup object:

alt text

Solution 7 - C#

That's odd. Does your program compile and run successfully and only fail on 'Publish' or does it fail on every compile now?

Also, have you perhaps changed the file's properties' Build Action to something other than Compile?

Solution 8 - C#

I had this problem and its because I wasnt using the latest version of C# (C# 7.3 as of writing this) and I had to change the below internal access modifier to public.

internal class Program
 {
	public static void Main(string[] args)
	{
        //My Code
    }
  }

or ammend the .csproj to use the latest version of C# (this way meant I could still keep the above class as internal):

<PropertyGroup>
	<LangVersion>latest</LangVersion>
	<NoWin32Manifest>true</NoWin32Manifest>
</PropertyGroup>

Solution 9 - C#

For me same error occurred because I renamed the namespace of the Program class.
The "Startup object" field in "Application" tab of the project still referenced the old namespace. Selecting new startup object solved the problem and also removed the obsolete entry from that list.
You may also want to update "Default namespace" field in the same "Application" tab.

Solution 10 - C#

It is important that the Main method is placed in the class that is properly configured in Visual Studio as a start-up object:

  • Right-click your project in project explorer; choose "Properties..."
  • In the properties dialog, choose "Application" tab
  • In the application tab, choose SimpleAIMLEditor.Program from the drop-down for your start-up object

Now run your application again. The error will disappear.

Solution 11 - C#

What worked for me: Close MS Visual Studio, then start Visual Studio and open the solution. The error message was then gone.

Solution 12 - C#

What I found is that the Program.cs file was not part of the solution. I did an add existing item and added the file (Program.cs) back to the solution.

This corrected the error: Error 1 Program '..... does not contain a static 'Main' method suitable for an entry point

Solution 13 - C#

My problem is that I accidentally set the arguments for Main

static void Main(object value)

thanks to my refactoring tool. Took couple mins to figure out but should help someone along the way.

Solution 14 - C#

In my case (where none of the proposed solutions fit), the problem was I used async/await where the signature for main method looked this way:

static async void Main(string[] args)

I simply removed async so the main method looked this way:

static void Main(string[] args)

I also removed all instances of await and used .Result for async calls, so my console application could compile happily.

Solution 15 - C#

I had changed

public static void Main(string[] args)

to

public async static void Main(string[] args)

The problem is that it needs to be a task:

public async static Task Main(string[] args)

Solution 16 - C#

In my case it was an XUnit test library showing this error. I had mistakenly deleted the Microsoft.NET.Test.Sdk package.

Solution 17 - C#

In my dotnet core 3.1 project I had created a new class/file instead of Program.cs that contained the Main method.

I had to update the Startup object to my new class name in Project Properties

enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionadeenaView Question on Stackoverflow
Solution 1 - C#QuibblesomeView Answer on Stackoverflow
Solution 2 - C#Basheer AL-MOMANIView Answer on Stackoverflow
Solution 3 - C#Freek BosView Answer on Stackoverflow
Solution 4 - C#MrOnigiriView Answer on Stackoverflow
Solution 5 - C#Ali AhmadiView Answer on Stackoverflow
Solution 6 - C#Igal TabachnikView Answer on Stackoverflow
Solution 7 - C#configuratorView Answer on Stackoverflow
Solution 8 - C#PontiusTheBarbarianView Answer on Stackoverflow
Solution 9 - C#Roland PihlakasView Answer on Stackoverflow
Solution 10 - C#Sergey KalinichenkoView Answer on Stackoverflow
Solution 11 - C#James HirschornView Answer on Stackoverflow
Solution 12 - C#RichView Answer on Stackoverflow
Solution 13 - C#satyajitView Answer on Stackoverflow
Solution 14 - C#Abdul Rahman KayaliView Answer on Stackoverflow
Solution 15 - C#8protonsView Answer on Stackoverflow
Solution 16 - C#HHansenView Answer on Stackoverflow
Solution 17 - C#joym8View Answer on Stackoverflow