16

I can't figure what's my wrong with my code below.

When I try to compile I get the message:

does not contain a static 'main' method suitable for an entry point.

This is my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RandomNumberGenerator
{

public partial class Form1 : Form
{
    private const int rangeNumberMin = 1;
    private const int rangeNumberMax = 3;
    private int randomNumber;

public Form1()
{            
        randomNumber = GenerateNumber(rangeNumberMin, rangeNumberMax);
}

private int GenerateNumber(int min,int max)
    {
        Random random = new Random();
        return random.Next(min, max);
    }

private void Display(object sender, EventArgs e)
    {                       
        switch (randomNumber)
        {
            case 1:
            MessageBox.Show("A");
            break;
            case 2:
            MessageBox.Show("B");
            break;
            case 3:
            MessageBox.Show("C");
            break;
        }

    }           
}
}

Can someone please tell me where I've gone wrong.

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
user2483549
  • 161
  • 1
  • 1
  • 3

10 Answers10

22

Every C# program needs an entry point. By default, a new c# Windows Forms project includes a Program class in a Program.cs file:

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

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

You are probably missing this or deleted it.

Matt Houser
  • 33,983
  • 6
  • 70
  • 88
20

Your project must be created as an empty project. So the output type shows as Console Application. Change it to Class library, and it should work

ManJan
  • 3,939
  • 3
  • 20
  • 22
8

simple change in code. main method should be 'Main' (Capital M).

3

I just had this issue myself.

I created a winforms project, decided to refactor my code and the project would now not contain the UI, so I deleted the Program.cs and winforms files only to get the same error you were getting.

You either need to re add the static void main() method as Matt Houser mentioned above, or go into the project properties and change the output type in the Application tab to Class Library.

Purplegoldfish
  • 5,268
  • 9
  • 39
  • 59
2

I have also experienced this wrong. I changed the dropdown situated in Project properties/Application tab (Output type:). The original selected value was "Class Library" but i changed to "Windows Application" and found same error. Now resolved.

razamzai
  • 31
  • 1
2

I also faced the similar issue, and i wasted my 13 hours in finding the solution. Lastly, i got the sol...

Right click on the project --> Go to properties -->Change output type to "Class Library"

Vote me up it this helped you.

Aditya
  • 21
  • 1
2

My case was very strange - app built fine before, but in one moment I faced with same issue.

What I had : ConsoleApplication

public static async void Main(string[] args)

so my main is async, but return type is void, but must be Task

public static async Task Main(string[] args)

Worked brilliantly before with void, not sure what was trigger this scenario, where void is became not ok to build

¯ \ (ツ) / ¯

Nigrimmist
  • 10,289
  • 4
  • 52
  • 53
1

Late, but for me, it was a "Console Application" project type, but "Build Action" in the file properties was some set to "None". Changed it to "Compile" and it was fine.

Kevin S. Miller
  • 913
  • 1
  • 9
  • 21
0

Might be you deleted Program file from your project. Just add Program.cs file your problem get resolved

sachin
  • 222
  • 2
  • 3
0

in the Solution Explorer: Right click on Project-->select properties-->select application tab-->select output type as Class library and save it and build it.