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.
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.
What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
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();
}
}
Starting C# 9 the option to have a program without the Main method has been introduced. Instead of having to declare the Main method, you can now write code directly in a file outside of a class. For more details see Top-level statements - programs without Main methods.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With