No Main and class confusion in newer C# versions
I am confused about the newer C# versions... When I make a console app, it just starts like so:
As you see, theres no class or Main method. In Java, and old C# if I am correct, we always have to have a class with a main method for our code to run.
However, it seems that newer C# versions doesn't require this. Yet my confusion is:
- OOP is rather valuable in a lot of scenarios, so what do we do without classes?
- Is the Main method just in the first file we create? or where is it? it seems to run without it
30 Replies
Top-level statements - C# tutorial - C#
This tutorial shows how you can use top-level statements to experiment and prove concepts while exploring your ideas
essentially, in one file (Program.cs) in your application, you can ignore the namespace / class / Main method declaration
they'll be assumed and your app will still run fine, you can always just write them back in manually, or tick the option "Do not use top level statements" when creating the project in VS
So in the one first file, called EXACTLY Program.cs, we have an implicit main method. In that file, we have access to
args
(from the main method) even though we didn't declare it.
- This means we cannot call the Program.cs file in other code?
- Also, it means it is the only file that is like that - e.g. all other files needs to have a namespace/class?Also, it means it is the only file that is like that - e.g. all other files needs to have a namespace/class?Correct
Ah we can access the Program.cs if we declare a class in it, its just not required by default. or at least I think that is possible?
Hm, no it seems that its not reusable
anything one writes in that file
They in fact shouldnt even be in Program.csStill, it seems like
Program.cs
can't have classes (even if one shouldn't have it anyhow), since it seems to wrap everything in an implicit "Main" method
Top Level Statements work with any file nameSo if I have multiple files without an entry method (Main), as well as all of them being Top Level Statements, then I will get errors and it won't work? E.g. only one file is allowed to use Top Level Statements, if I didn't explcitly write a Main method myself somewhere? Aha, so if I make a class (or similar things) then it auto assumes that this should not be included in the Main Method. Its smart enough to differentiate between Top Level Statements, and things like declaring classes which are not a Top Level Statement
It is recommended to not add a bunch of classes to your top-level-statements file, since it gets messy
but you can.
for a tiny console app, you might stick some records in there
the order is also very important
Right, so I can but it is not recommended.
If I have a Main method explictly declared somewhere (not Program.cs), can I then have multiple Top Level Statement files?
if you declare anything before a statement is used, the compiler disabled top level mode
no
Ah makes sense
if you have an explicit Main, you cant have any TLS files
Aaah
please dont try and "abuse" TLS
its meant for simple short startup methods
now I get it. So ONE file with TLS at MOST, and it is not recommended to include extra things like classes etc in that file
like, creating a host builder, configuring it, building it, running the host
pretty much yep
Thanks
makes sense
:)
ohhh sorry, one last quesiton. Is the TLS file always included in the Namespace and/or solution that I am writing in?
nope
it has a global namespace iirc
ok thanks
you should never have to refer back to it however
so it shouldnt be a problem
yea true
thanks
the only "smart" thing the compiler does with TLS files is figure out if args should be available or not, and if it returns a void or an int
oh and if it should be
async Task
or notTrue, this thing messed with me also that in C# we can have main method with int or void
lol
it just "picks" the "right" main from the list of $mains
The possible signatures for
Main
are public
is not required (can be any accessibility).
Top-level statements are compiled into a Main
method and will use an appropriate signature depending on the body.
https://docs.microsoft.com/en-US/dotnet/csharp/fundamentals/program-structure/main-command-lineMain() and command-line arguments - C#
Learn about Main() and command-line arguments. The 'Main' method is the entry point of an executable program.
lol
wow
99% of the time void is fine - int is useful if you want to explicitly control the errorcode
hm main method seem tougher than I thought to learn. I am just used to one main method in Java (perhaps its same there, but I am ignorant of it)
useful for stuff like kubernetes
its the same really
Yeah ok
then I won't have to worry, all of my code so far is just using main method returning void