你好,世界! - C#

时间:2012-11-18 16:12:12

标签: c#

我不确定我在这里做错了什么。我试过Console.Read();, Console.ReadLine();,也没有。我也试过Ctrl F5。我在搜索时没有找到其他建议。我正在使用Visual Studio Express,如果它是相关的。显然,我想让程序说“你好世界!”这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Miscellaneous
{
    class Hello_World_
    {
        static void Main()
        {
            Console.WriteLine("Hello world!");
            Console.Read();
        }
    }
}

错误1 Program 'c:\Users\Evorlor\Desktop\Visual Studio Workspace\Miscellaneous\Miscellaneous\obj\Debug\Miscellaneous.exe' has more than one entry point defined: 'Miscellaneous.Hello_World_.Main()'. Compile with /main to specify the type that contains the entry point. C:\Users\Evorlor\Desktop\Visual Studio Workspace\Miscellaneous\Miscellaneous\Hello World!.cs 11 21 Miscellaneous

错误2 Program 'c:\Users\Evorlor\Desktop\Visual Studio Workspace\Miscellaneous\Miscellaneous\obj\Debug\Miscellaneous.exe' has more than one entry point defined: 'Miscellaneous.Program.Main()'. Compile with /main to specify the type that contains the entry point. C:\Users\Evorlor\Desktop\Visual Studio Workspace\Miscellaneous\Miscellaneous\Program.cs 15 21 Miscellaneous

2 个答案:

答案 0 :(得分:3)

该方法必须以大写字母M Main调用,作为该程序的“入口点”。

此外(感谢评论),最后一个using指令指向一个不存在的命名空间。 mscorlib程序集中存在哪些名称空间取决于您正在使用的.NET版本..您需要的唯一usingusing System;

此外,根据您的问题更新,您在同一解决方案中有两个包含Main()方法的类,即class Hello_World_class Program。要编译,您必须设置哪个Main()是启动方法(入口点)。在Visual Studio的右侧,右键单击解决方案中要设置为启动项目的项目(或类)。

答案 1 :(得分:1)

您可能没有为项目使用控制台应用程序模板。 (Here's how that works when creating a new project

如果您没有使用上述方法创建项目,请确保程序的输出类型设置为控制台应用程序,并且启动对象设置为Miscellaneous.Hello_World_

您可以在项目属性中设置输出类型启动对象Here's how