123

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.

atwellpub
  • 5,660
  • 11
  • 38
  • 52
  • 13
    do you have a main method?? – bobek Mar 07 '12 at 19:22
  • To be honest I don't see a method called Main anywhere in my code. I've been working on this project for a couple of months now too. – atwellpub Mar 07 '12 at 19:29
  • 1
    You need a static method called `main`, with the correct signature. That's how the compiler knows how to start your program. – David Heffernan Mar 07 '12 at 19:30
  • Is there a way I can manually program the solution where to start? I'm confused to how it cannot find a static 'Main' method when I can't seem to find any instruction in the code telling it to look for a 'Main' method. I am very new at c#. --- [edit] - @David Hefferman , Would you elaborate on what you mean by 'the correct signature'? – atwellpub Mar 07 '12 at 19:32
  • The way you tell the compiler where your program starts is by adding a static function named `main` with the appropriate signature. – David Heffernan Mar 07 '12 at 19:33
  • 6
    try adding something like this to your project `[STAThread] static void Main(string[] args) { Application.Run(new shell()); }` – L.B Mar 07 '12 at 19:35
  • Hey @L.B Alright! Worked and I'm back in business. Thanks to everyone that helped! – atwellpub Mar 07 '12 at 19:44
  • 1
    @L.B: Create that as answer so he can accept it. – Joshua Mar 07 '12 at 20:02

33 Answers33

173

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.

SuperDJ
  • 7,488
  • 11
  • 40
  • 74
Arne
  • 1,755
  • 1
  • 10
  • 2
  • 1
    A bit overkill of a solution, I think. It would be a lot easier to follow @eyossi's solution. – TimWagaman Jun 07 '12 at 15:38
  • 2
    But did you want a console application or a class library? Don't just change the type of application to fix an error; know what the error is telling you. Console applications need an entry point (`static void main()`) to determine where to start executing code; class libraries don't - they are called by other libraries that are already executing. – KyleMit Feb 05 '14 at 16:31
  • This is the overkill version of what @pixaloop suggests. After reading this answer, I remembered that when adding an Empty project defaults the output to Console. So I just went and changed it. Wanted to post it as an answer (as it is much simpler), but saw that someone else already did. – EternalWulf Oct 03 '14 at 11:36
  • Thanks man. That would have been the last place I would have looked! – eaglei22 Jul 16 '15 at 19:55
  • 1
    Not overkill at all. This was the exact issue in my situation. The compiler was looking for a `main` in my two other projects that were *Console Application*s just like the StartUp project. Changing those two "assisting" projects to *Class Library* in their Properties screen fixed it immediately. – Ctrl S Nov 20 '18 at 18:23
111

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”.

Automationtested
  • 1,234
  • 1
  • 8
  • 10
  • 2
    This is exactly what I did, works perfect. If you added an 'Empty' project, it defaults to Console. – EternalWulf Oct 03 '14 at 11:35
  • Thank you, this worked perfectly for me. One more thing I would like to add is that, when you right click your project and go to the properties, the "Output Type" will be available under "General". So the whole path is basically [Your Project Name] > Properties > Application > General > Output Type – Brookie_C Jan 14 '22 at 20:58
42

I had this error and solved it using this solution.

  1. Right click on the project
  2. Select "Properties"
  3. Set "Output Type" to "Class Library".
pushkin
  • 9,575
  • 15
  • 51
  • 95
Ali Ahmadi
  • 563
  • 5
  • 9
28

Try adding this method to a class and see if you still get the error:

[STAThread]
static void Main()
{
}
KyleMit
  • 30,350
  • 66
  • 462
  • 664
eyossi
  • 4,230
  • 22
  • 20
  • In my case, I wanted a console app. So adding the entry point to my class did the trick. I'm a VS 2022 noob but I liked the old console templates better. This is confusing and console apps should just work out of the box without writing any code. Bad UI decision by Microsoft IMO. – Vance McCorkle Feb 16 '22 at 05:45
  • This helped me. My Main() method wasn't static. – tolache Aug 03 '22 at 08:00
15

Edit .csproj file

<OutputType>Library</OutputType>

cheers !

bereket gebredingle
  • 12,064
  • 3
  • 36
  • 47
  • 1
    This actually works, but it has to be `Library` (with a capital `L`) – detunized Dec 08 '19 at 11:26
  • 2
    It is supposed to be the default in .net core https://learn.microsoft.com/fr-fr/visualstudio/msbuild/common-msbuild-project-properties?view=vs-2019 Turns out that when your project is the default value Library is overriden to Exe during build. Resolving SDK 'Microsoft.NET.Sdk.Web.ProjectSystem'... Property reassignment: $(OutputType)="Exe" (previous value: "Library") – Romain Hautefeuille May 28 '20 at 10:01
  • 2
    Instead of editing .csproj, you can use the GUI in Visual Studio 2019. 1. Select the project in the Solution Explorer 2. Click "Project" --> " Properties" from the main toolbar 3. Output type: Class Library 4. Save – Joshua Swain Dec 11 '20 at 14:43
14

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());
         }
     }

 }
KyleMit
  • 30,350
  • 66
  • 462
  • 664
ROM
  • 227
  • 2
  • 5
  • 11
14
  1. Select App.xaml and display its properties. Set Build Action to ApplicationDefinition.
  2. 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.
Roman Volkov
  • 389
  • 1
  • 3
  • 8
12

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.

Jan
  • 2,178
  • 3
  • 14
  • 26
  • I had the same, I think because I initially created the solution with VS2019 and then tried to open it in VS2017. Mine is a dotnetcore console app. – trebor Jul 28 '20 at 14:04
  • Add inside https://codez.deedx.cz/posts/csharp-async-main/ – user1220497 Mar 18 '21 at 06:03
8

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".

simonbs
  • 7,932
  • 13
  • 69
  • 115
  • Thank you! Perhaps in another couple of hours, I'd realize that I couldn't generate a syntax error and thus looked there.. – Gerard ONeill Feb 16 '16 at 23:33
7

Make sure you are not using void with async like

static async void Main(string[] args)

If yes, then change void to Task like

static async Task Main(string[] args)
Dour High Arch
  • 21,513
  • 29
  • 75
  • 90
Gaurang Naik
  • 83
  • 1
  • 4
6

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.

Moondog 2112
  • 81
  • 1
  • 3
4

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..

2

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 2017


Visual Studio 2019

Visual Studio 2019

Ali Jamal
  • 1,383
  • 1
  • 13
  • 20
2

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

Siddhi Kamat
  • 135
  • 2
  • 11
  • How do you set startup object in VSCode ?? – Sold Out Jun 11 '21 at 15:33
  • 1
    @SoldOut This might help you [visual-studio-code-program-has-more-than-one-entry-point-defined](https://stackoverflow.com/questions/47152454/visual-studio-code-program-has-more-than-one-entry-point-defined) – Siddhi Kamat Aug 06 '21 at 17:14
  • Inerestin'... thank you :) But why in the world does it erase your name with '@' ?? – Sold Out Aug 09 '21 at 07:22
2

enter image description here

Just right click on project and select properties and then set Output type on Class Library

Ata Hoseini
  • 117
  • 1
  • 4
2

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).

Joshua
  • 40,822
  • 8
  • 72
  • 132
  • I'd be interested in hearing a little more about Application Framework setting and anything that might tell the software to look for Main() when a Main method did not seem to exist anywhere. – atwellpub Mar 07 '12 at 19:46
  • I believe the Application Framework setting only applies to VB.NET. C# Forms applications always have "main" as an entry point. – A. Wilson Mar 07 '12 at 19:59
1

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.

Gerrie Pretorius
  • 3,381
  • 2
  • 31
  • 34
1

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

Madison Haynie
  • 448
  • 4
  • 13
1

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");

            }
        }
}
yash patel
  • 19
  • 1
  • 4
1

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:

  1. Set Project -> Properties -> Application -> Output type = Console Application
  2. Create the /App_Code/Main.cs
  3. Add the code above in it (and reference the methods in my project)
  4. 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.

1

If you are using a class library project then set Class Library as output type in properties under application section of project.

Manveer Singh
  • 351
  • 6
  • 27
1

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.

smoksnes
  • 10,509
  • 4
  • 49
  • 74
1

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.

enter image description here

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 19 '21 at 21:09
1

I got this error when using the command Build Docker Image in Visual Studio 2022.

error CS5001: Program does not contain a static 'Main' method suitable for an entry point

The project built perfectly well in Windows but I tried to build a Linuxcontainer. Switching to Output Type Class Library solved the error but Docker Compose gave me this error instead:

CTC1031 Linux containers are not supported for

https://stackoverflow.com/a/74044317/3850405

I tried explicitly using a Main method like this but it did not work:

namespace WebApplication
{
    public class Program
    {
        public static void Main(string[] args)
        {

I have no idea why but this solved it for me:

Gives error:

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["src/Services/Classification/ClassificationService.Api/ClassificationService.Api.csproj", "Services/Classification/ClassificationService.Api/"]

RUN dotnet restore "Services/Classification/ClassificationService.Api/ClassificationService.Api.csproj"
COPY . .
WORKDIR "/src/Services/Classification/ClassificationService.Api"
RUN dotnet build "ClassificationService.Api.csproj" -c Release -o /app/build

Works:

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["src/Services/Classification/ClassificationService.Api/ClassificationService.Api.csproj", "src/Services/Classification/ClassificationService.Api/"]

RUN dotnet restore "src/Services/Classification/ClassificationService.Api/ClassificationService.Api.csproj"
COPY . .
WORKDIR "/src/src/Services/Classification/ClassificationService.Api"
RUN dotnet build "ClassificationService.Api.csproj" -c Release -o /app/build

Notice the double /src in the working example.

I read that you had to place the Dockerfile at the same level as .sln file but in my case the files are separated by four levels.

https://stackoverflow.com/a/63257667/3850405

Ogglas
  • 62,132
  • 37
  • 328
  • 418
0

I too have faced this problem. Then I realized that I was choosing Console Application(Package) rather than Console Application.

Prasad
  • 866
  • 1
  • 12
  • 15
0

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.

MaChaToc
  • 139
  • 1
  • 12
0

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.

grumpy
  • 31
  • 1
  • 6
0

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.

James Wierzba
  • 16,176
  • 14
  • 79
  • 120
0

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)
        {
        }
    }
}
Adam Cox
  • 3,341
  • 1
  • 36
  • 46
0

If you use Visual Studio Code change Project Sdk="Microsoft.NET.Sdk.Web" to Project Sdk="Microsoft.NET.Sdk" on csproj file.

Elseif
  • 1
  • 1
0

Add

static async Task Main(string[] args)
{
}

instead of

static async void Main(string[] args)
{
}

its work for me.

Ishara Samintha
  • 460
  • 6
  • 8
0

If you do indeed have a public static main method it could be your build settings as explained in this question: Troubleshooting "program does not contain a static 'Main' method" when it clearly does...?

Community
  • 1
  • 1
A. Wilson
  • 688
  • 1
  • 6
  • 15
-1

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

ririvas
  • 11