Does not contain a static 'main' method suitable for an entry point
C#Visual Studio-2010C# Problem Overview
I began organizing my code to day into seperarate .cs files, and in order to allow the methods that work with the UI to continue to do so I would create the .cs code under the same namespace and public partial class name so the methods could be inter-operable.
My header look like this in four files, including my main core file that calls:
public shell()
{
InitializeComponent();
}
Header area of .cs files that work with the UI (and seem to be causing this new conflict):
using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Data.SqlServerCe;
using System.Diagnostics;
using System.Threading;
using System.Collections.Specialized;
using System.Net;
using System.Runtime.InteropServices;
using watin = WatiN.Core;
using WatiN.Core.Native.InternetExplorer;
using System.Web;
namespace WindowsFormsApplication1
{
public partial class shell : Form
{
Now when I try to debug/preview my application (BTW this is a Windows Application within Visual Studio 2010 Express) I get this error message:
> Does not contain a static 'main' method suitable for an entry point
I looked in the application properties in Application->Startup object, but it offers me no options. How can I inform the application to begin at the .cs file that has my InitializeComponent(); command?
- I've looked around so far without a solution.
- The properties on each .cs file are set to 'Compile'.
- I do not see an App.xaml file in my Solutions explorer but I do see a app.config file.
I'm still very new and this is my first attempt at an organizing method with c# code.
C# Solutions
Solution 1 - C#
I was looking at this issue as well, and in my case the solution 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 2 - C#
Change the Output Type under the Project > Properties to that of a “Class Library”. By default, this setting may have been set to a “Console Application”.
Solution 3 - C#
I had this error and solved it using this solution.
- Right click on the project
- Select "Properties"
- Set "Output Type" to "Class Library".
Solution 4 - C#
Try adding this method to a class and see if you still get the error:
[STAThread]
static void Main()
{
}
Solution 5 - C#
If you don't have a file named Program.cs
, just add a new Class and name it Program.cs
.
Then paste this code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Sales {
static class Program {
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
Solution 6 - C#
- Select App.xaml and display its properties. Set Build Action to ApplicationDefinition.
- App.xaml and its corresponding *.cs file must be placed into the root directory of the *.csproj file, i. e. not into a "Source" folder.
Solution 7 - C#
Had this problem in VS 2017 caused by:
static async Task Main(string[] args)
(Feature 'async main' is not available in C# 7.0. Please use language version 7.1 or greater)
Adding
<LangVersion>latest</LangVersion>
to app.csproj helped.
Solution 8 - C#
If you do have a Main method but still get this error, make sure that the file containing the Main method has "Build action" set to "Compile" and "Copy to ouput directory" set to "Do not copy".
Solution 9 - C#
Edit .csproj file
<OutputType>Library</OutputType>
cheers !
Solution 10 - C#
For me, the error was actually produced by "Feature 'async main' is not available in C# 7.0. Please use language version 7.1 or greater". This issue was resulting in the "Does not contain a static 'main' method suitable for an entry point" message in the Error List, but the Output window showed the "not available" error. To correct this, I changed the language version from 'C# latest minor version (default)' to 'C# latest minor version (latest)' under Advanced Build Settings.
Solution 11 - C#
hey i got same error and the solution to this error is just write Capital M instead of small m.. eg:- static void Main() I hope it helps..
Solution 12 - C#
Looks like a Windows Forms project that is trying to use a startup form but for some reason the project properties is set to startup being Main.
If you have enabled application framework you may not be able to see that Main is active (this is an invalid configuration).
Solution 13 - C#
Salaam,
I have both Visual Studio 2017
and Visual Studio 2019
Visual Studio 2019 does not show this error but 2017 does. Try Installing Visual Studio 2019.
Visual Studio 2017
Visual Studio 2019
Solution 14 - C#
After placing the above code in Program.cs, follow below steps
> 1. Right click on the project
>
> 2. Select Properties
>
> 3. Set Output Type
to Windows Application
>
> 4. Startup object : namepace.Program
Solution 15 - C#
When you want to allow paramaters to be specified from the command, they must look like this:
[STAThread]
static void Main(params string[] paramaters)
{
you cannot specify more than one paramater, otherwise this will also cause the error reported above.
Solution 16 - C#
For some others coming here:
In my case I had copied a .csproj from a sample project which included <EnableDefaultCompileItems>false</EnableDefaultCompileItems>
without including the Program.cs file. Fix was to either remove EnableDefaultCompileItems or include Program.cs in the compile explicitly
Solution 17 - C#
hellow your main class was deleted so add new class that name set as Main.cs and pest that code or if porblem in window so same problem on that
using System;
using System.Collections.Generic;
using System.Linq;
using Foundation;
using UIKit;
namespace your_PKG_name.iOS
{
public class Application
{
// This is the main entry point of the application.
static void Main(string[] args)
{
// if you want to use a different Application Delegate class from "AppDelegate"
// you can specify it here.
UIApplication.Main(args, null, "AppDelegate");
}
}
}
Solution 18 - C#
A valid entry looks like:
public static class ConsoleProgram
{
[STAThread]
static void Main()
{
Console.WriteLine("Got here");
Console.ReadLine();
}
}
I had issues as I'm writing a web application, but for the dreadly loading time, I wanted to quickly convert the same project to a console application and perform quick method tests without loading the entire solution.
My entry point was placed in /App_Code/Main.cs, and I had to do the following:
- Set Project -> Properties -> Application -> Output type = Console Application
- Create the /App_Code/Main.cs
- Add the code above in it (and reference the methods in my project)
- Right click on the Main.cs file -> Properties -> Build Action = Compile
After this, I can set the output (as mentioned in Step 1) to Class Library to start the web site, or Console Application to enter the console mode.
Why I did this instead of 2 separate projects?
Simply because I had references to Entity Framework and other specific references that created problems running 2 separate projects.
For easier solutions, I would still recommend 2 separate projects as the console output is mainly test code and you probably don't want to risk that going out in production code.
Solution 19 - C#
If you are using a class library project then set Class Library as output type in properties under application section of project.
Solution 20 - C#
Another situation where this occur is when someone (unintentionally) changes Build Action for Program.cs
. The value for Build Action should be C# compiler
.
I accidentally changed Build Action to None
, which removed program.cs
from the project and therefore wasn't included when compile started.
Solution 21 - C#
If you do indeed have a public static main method it could be your build settings as explained in this question: https://stackoverflow.com/questions/386831/troubleshooting-program-does-not-contain-a-static-main-method-when-it-clearl
Solution 22 - C#
I too have faced this problem. Then I realized that I was choosing Console Application(Package) rather than Console Application.
Solution 23 - C#
I am using Visual Studio and also had this problem. It took me some time, but in my program it was caused because I accidentally deleted a Class named "Program" that is generated automatically.
Solution 24 - C#
For future readers who faced same issue with Windows Forms Application, one solution is to add these lines to your main/start up form class:
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MyMainForm());
}
Then go to project properties > Application > Startup Object dropdown, should see the namespace.MyMainForm, select it, clean and build the solution. And it should work.
Solution 25 - C#
Check to see if the project is set as the "Startup Project"
Right click on the project and choose "Set as Startup Project" from the menu.
Solution 26 - C#
If you are like me, then you might have started with a Class Library, and then switched this to a Console Application. If so, change this...
namespace ClassLibrary1
{
public class Class1
{
}
}
To this...
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
}
}
}
Solution 27 - C#
Solution 28 - C#
If you use Visual Studio Code change Project Sdk="Microsoft.NET.Sdk.Web" to Project Sdk="Microsoft.NET.Sdk" on csproj file.
Solution 29 - C#
Did you accidentally remove the entire Program.cs file? If you have removed,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ListWievKullanımı
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
This might work for you. Program.cs
Solution 30 - C#
Perhaps unintentional, but moving my docker file to the solution folder instead of the project eliminated the error. This was helpful when I still wanted to run the solution independently of docker