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



我不知道下面的代码出了什么问题。

当我尝试编译时,我得到消息:

不包含适合于入口点的静态'main'方法。

这是我的代码:

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

谁能告诉我我哪里做错了

每个c#程序都需要一个入口点。默认情况下,新的c# Windows窗体项目在Program.cs文件中包含一个Program类:

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

您可能错过了这个或删除了它

必须将项目创建为空项目。因此输出类型显示为Console Application。将其更改为Class library,它应该可以工作

简单修改代码。main方法应该是' main '(大写M)

我自己也遇到过这个问题。

我创建了一个winforms项目,决定重构我的代码,该项目现在将不包含UI,所以我删除了Program.cs和winforms文件,只是为了得到相同的错误。

您需要像Matt Houser上面提到的那样重新添加静态void main()方法,或者进入项目属性并将Application选项卡中的输出类型更改为Class Library。

我也经历过这种错误。我改变了位于项目属性/应用程序选项卡中的下拉菜单(输出类型:)。原来选择的值是"类库",但我改为"Windows应用程序",发现同样的错误。现在解决。

我也遇到过类似的问题,我浪费了13个小时去寻找解决方案。最后,我得到了sol…

右键单击项目——>将输出类型更改为"Class Library"

如果这对你有帮助就给我投票。

我的情况很奇怪-应用程序之前构建得很好,但在某一刻我面临同样的问题。

我曾经: ConsoleApplication

public static async void Main(string[] args)

所以我的main是async,但是返回类型是void,但是必须是Task

public static async Task Main(string[] args)

以前在void中工作得很好,不确定是什么触发了这个场景,在这里void变得不适合构建

¯ ()

晚了,但对我来说,这是一个"控制台应用程序"项目类型,但文件属性中的"构建操作"被设置为"None"。改成"Compile"就可以了

可能是您从项目中删除了Program文件。只要添加Program.cs文件,你的问题就解决了

在解决方案资源管理器中:右键单击项目—>选择属性—>选择应用程序选项卡—>选择输出类型为类库并保存并构建。

相关内容

最新更新