Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where should I place public static void Main?

Tags:

c#

I am currently reading the Microsoft Official Course books for C# programming, the first concept they introduce you to is Console.WriteLine

The actual code they give you to type is:

class Hello
{
    public static void Main()
    {
       Console.WriteLine("Hello, World");
    }
}

I am not sure whether I am supposed to place this code under Form Load or using System because I always get the following error(s) and I am not sure what it implies:

Error ...Debug\WindowsFormsApplication1.exe' has more than one entry point defined: 'Hello.Main()'. Compile with /main to specify the type that contains the entry point.**

like image 913
Alee321 Avatar asked Aug 06 '13 20:08

Alee321


2 Answers

That is code for a console application, not a WinForm application. In Visual Studio, create a new project and select Console Application as the project type.

You'll notice that a new console project will have most of the code you've got written for you (e.g. the Main method)

enter image description here

The reason you were getting that error was because WinForm already has a Main method in Program.cs. The Main method is known as the entry point and .net standalone exe projects have one. You can make the entry point another method but this is rarely done as there's no real need to in the majority of cases.

like image 88
keyboardP Avatar answered Oct 07 '22 03:10

keyboardP


You have the wrong application type.

The course probably mentions somewhere that you should create a new Console Application. You have chosen Windows Forms in your attempt.

Try creating a new Console Application, and the Main method will already be defined for you.

like image 42
Khan Avatar answered Oct 07 '22 02:10

Khan