C # class without main method

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

What if I want to create a class that is not a main class, i.e. the one I import into the main class?

I tried to do this, and when I compile (using cmd with csc File.cs ), the compiler says that the .exe file it will make does not have a main() method. Does this mean that I was wrong and that every class needs the main() method, or that I am compiling it incorrectly?

Maybe the problem is in the code (as I rely 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 do not ask if the file needs the main method, I ask how I can import this class into another class, because I understand that if I do this, I will not have the main thing (as I said, I have some Java experience ), but whenever I try to compile without it, the compiler tells me that I need it.

+8
c # main class
source share
9 answers


Not all classes need the Main method.

As MSDN states

The main method is the entry point to the C # console application or window. (Libraries and services do not require a method as an entry point.). When the application starts, the main method is the first method that is called.

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


Only one class must support the Main method, a class that acts as the entry point of the application.

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

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


In the 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 this class, you can create a static class using the main method:

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

In this scenario, you need at least one class in your code using the Main method. Other classes do not need the Main method.

+1
source share

static void main(string[] args) method in C # programs is the starting point for execution. If you try to compile a single C # file, the compiler will find this method to start execution. You do not need to create a method in the class that you use as a model, but you must have this method in the console program, WinForms, etc.

0
source share

You only need the main method when building the executable assembly (.exe), and you only need it in one class. This method will be the default entry point where execution begins. When creating code as a class library (.dll), you do not need the main method.

0
source share

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

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

0
source share

A C # application must have at least one class using the Main method, so execution can begin with it. An application can have many classes, but only one class with one main method is required.

C # library should not have a Main method.

0
source share

If this is a console application, first you need the Main method. Otherwise (for example, a web application) you do not need.

0
source share

Only one class with one method should be ok. If you want, you can configure the starting object in visual studio in the settings.

0
source share

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

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

make yourMainClassFileName.exe file

0
source share

All Articles