C# class without main method

若如初见. 提交于 2019-12-05 01:46:56
PaRiMaL RaJ


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();
    }
}

In this scenario you need at least one class in your code with the Main method in it. The other classes do not need the Main method.

C# application must have at least one class with Main method, so that execution can begin from it. Application can have plenty of classes, but only one class with only one Main method is required.

C# library does not have to have a Main method.

If this is a console application, you need a Main method to start with. Otherwise (web application, for example), you don't need one.

Try using /t:library switch with the compiler. By default it tries to make an .exe which, of course, needs an entry point (i.e. a main method). If you compile to a .dll you won't need that.

But as HighCore suggested, if you are learning, just use Visual Studio (download one of the free versions if you haven't already) and let it worry about the compiler flags.

You only need a main method when you build an executable assembly (.exe), and you only need it in one class. This method will be the default entry point where execution starts. You don't need a main method when you build your code as a class library (.dll).

Only one class with one method should be fine. If you want, you can set up the start up object in visual studio in the settings.

C# class without Main() means, you can compile it as a library (.dll) csc /target:library YourClassFileName.cs or csc /t:library YourClassFileName.cs to make it as YourClassFileName.dll file and then you can use it in another class file which have the Main() method (Entry point)

csc /reference:YourClassFileName.cs YourMainClassFileName.cs or csc /r:YourClassFileName.cs YourMainClassFileName.cs

to make an YourMainClassFileName.exe file

static void main(string[] args) method in C# programs is the start point to execute. If you try to compile a single C# File, the compiler will find this method to start the execution. You don't need to create the method in a class you are using as a model, but you have to have this method on a Console Program, WinForms, etc...

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!