简体   繁体   中英

C# beginner program

I'm using Visual Studio and when I try to compile the code below I get two errors;

CS0103 The name 'Console' does not exist in the current context

CS0017 C# Program has more than one entry point defined. Compile with /main to specify the type that contains the entry point.

How do I fix this?

using System;

namespace App2
{
    class Class1
    {
        static void Main()
        {
            Console.Writeline("Hello");
        }
    }
}

It looks like Class1 is not the only class that has a static void Main() method defined. Usually, when you create a console application, there's a class called "Program" that already contains a method Main . There should be no need to add another class.

Just modify the existing Main method. This should solve the second error.

The Main method is a bit like the Highlander of methods - there can only be one.

As for the first error: You do need to target the Windows platform to be able to use Console , so you need to create a Console application or a Windows Forms/WPF application or the like.

As Thorsten Dittmar said , ensure you don't have a duplicate static void Main method,

CS0103 The name 'Console' does not exist in the current context

Ensure you did not create an Universal Windows Platform (UWP) project/Android. You can't call Console on it.

The first error is caused by a typo. To correct it, change Writeline to WriteLine (with a capital letter L).

The second error is caused by the fact that you did not specify clearly which entry point the program should use. To fix this, follow these steps:

Right-click on your project in Solution Explorer , and open the "Properties" menu. You will see the similar page and all that you should do is explicitly select entry-point.

在此处输入图片说明

As many have suspected I fixed the error by going into the installer, modified and checked off the .NET Desktop Development .

It works now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM