Understanding the Main Method

Every C# application must contain a method called Main. This is the entry point for the application to run. You can place this method within any class in your project because the compiler is smart enough to look for it when the time arrives.

The Main method has two special requirements that must be met before an application can work correctly. First, the method must be declared as public. This ensures that the method is accessible. Second, the method must be declared as static. The static keyword ensures that only one copy of the method can be loaded at any given time.

With those rules in mind, look at the following code:

class Class1 {

public static void Main() {

As you see, this example has one class called Classl. This class contains the Main method into the application. This Main method is where you place all the code to run your application. Although it is OK to put this method in the same class and file as the rest of the code in your application, it's generally a good idea to create a separate class and file for the Main method. This helps other developers who may need to deal with your code.

0 0

Post a comment