'C# class without main method

I'm learning C# and I'm very new to it, so forgive me for the seemingly stupid question. I have some experience in Java, and I noticed that C# programs also need a main() method in their main class.

What if I want to create a class that isn't a main class, i.e. one that I import into a main class?

I tried to do that, and when I compile (via cmd using csc File.cs) the compiler says that the .exe that it will make has no main() method. Does that mean that I was wrong, and every class needs a main() method, or that I'm compiling it wrongly?

Maybe the problem's in the code (since I'm relying on my knowledge of Java syntax), which looks like this:

public class Class
{
    int stuff;
    public Class(int stuff)
    {
        this.stuff = stuff;
        stuff();
    }
    public void method()
    {
        stuff();
    }
}

EDIT: I'm afraid this is terribly misunderstood. I'm not asking if the file needs a main method, I'm asking how I can import this class into another class, because I realise that if I am to do this I can't have a main (as I said, I have some Java experience), but whenever I try to compile without one the compiler tells me that I need one.



Solution 1:[1]

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.

Solution 2:[2]

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.

Solution 3:[3]

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

Solution 4:[4]

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.

Solution 5:[5]

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).

Solution 6:[6]

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.

Solution 7:[7]

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

Solution 8:[8]

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...

Solution 9:[9]

Main is required, but C#9 has this feature such that you will not write Main but it will act as Main.

In C# 9.0 you can just choose to write your main program at the top level instead:

using System;

Console.WriteLine("Hello World!");

Any statement is allowed. The program has to occur after the usings and before any type or namespace declarations in the file, and you can only do this in one file, just as you can have only one Main method today.

If you want to return a status code you can do that. If you want to await things you can do that. And if you want to access command-line arguments, args is available as a “magic” parameter.

Local functions are a form of statement and are also allowed in the top-level program. It is an error to call them from anywhere outside of the top-level statement section.

Reference: https://devblogs.microsoft.com/dotnet/welcome-to-c-9-0/#top-level-programs

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 chue x
Solution 2 Dialecticus
Solution 3 Robert
Solution 4
Solution 5
Solution 6 kittu
Solution 7 Abdul Karim Siddiqui
Solution 8 Felipe Oriani
Solution 9