C# class without main method


Not all classes need Main method.

As MSDN States

The Main method is the entry point of a C# console application or windows application. (Libraries and services do not require a Main method as an entry point.). When the application is started, the Main method is the first method that is invoked.

There can only be one entry point in a C# program. If you have more than one class that has a Main method, you must compile your program with the /main compiler option to specify which Main method to use as the entry point.


Only one class need to keep the Main method, the class which acts as entry point of the application.

The signature of the main method is : static void Main(string[] args) or static void Main() or static int Main(string[] args) or static int Main()

Check out this link for more details : Main() and Command-Line Arguments (C# Programming Guide)


For your above example:

public class MyClassName // changed the class name, avoid using the reserved keyword :P
{
    int stuff;
    public MyClassName(int stuff)  // is the constructor
    {
        this.stuff = stuff;
    }
    public void method()
    {
        stuff = 1;
    }
}

If you need to use that class, you can create a static class with main method:

class ProgramEntry
{
    static void Main(string[] args)
    {
        MyClassName classInstance = new MyClassName(2);
        classInstance.method();
    }
}

Tags:

C#

Class

Main