C
C#10mo ago
Steadhaven

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:
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
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
SinFluxx
SinFluxx10mo ago
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
SinFluxx
SinFluxx10mo ago
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
Steadhaven
SteadhavenOP10mo ago
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?
SinFluxx
SinFluxx10mo ago
Also, it means it is the only file that is like that - e.g. all other files needs to have a namespace/class?
Correct
Steadhaven
SteadhavenOP10mo ago
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?
Steadhaven
SteadhavenOP10mo ago
No description
Steadhaven
SteadhavenOP10mo ago
Hm, no it seems that its not reusable anything one writes in that file
They in fact shouldnt even be in Program.cs
Still, 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 name
So 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
Pobiega
Pobiega10mo ago
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
Steadhaven
SteadhavenOP10mo ago
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?
Pobiega
Pobiega10mo ago
if you declare anything before a statement is used, the compiler disabled top level mode no
Steadhaven
SteadhavenOP10mo ago
Ah makes sense
Pobiega
Pobiega10mo ago
if you have an explicit Main, you cant have any TLS files
Steadhaven
SteadhavenOP10mo ago
Aaah
Pobiega
Pobiega10mo ago
please dont try and "abuse" TLS its meant for simple short startup methods
Steadhaven
SteadhavenOP10mo ago
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
Pobiega
Pobiega10mo ago
like, creating a host builder, configuring it, building it, running the host pretty much yep
Steadhaven
SteadhavenOP10mo ago
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?
Pobiega
Pobiega10mo ago
nope it has a global namespace iirc
Steadhaven
SteadhavenOP10mo ago
ok thanks
Pobiega
Pobiega10mo ago
you should never have to refer back to it however so it shouldnt be a problem
Steadhaven
SteadhavenOP10mo ago
yea true thanks
Pobiega
Pobiega10mo ago
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 not
Steadhaven
SteadhavenOP10mo ago
True, this thing messed with me also that in C# we can have main method with int or void lol
Pobiega
Pobiega10mo ago
it just "picks" the "right" main from the list of $mains
MODiX
MODiX10mo ago
The possible signatures for Main are
public static void Main() { }
public static int Main() { }
public static void Main(string[] args) { }
public static int Main(string[] args) { }
public static async Task Main() { }
public static async Task<int> Main() { }
public static async Task Main(string[] args) { }
public static async Task<int> Main(string[] args) { }
public static void Main() { }
public static int Main() { }
public static void Main(string[] args) { }
public static int Main(string[] args) { }
public static async Task Main() { }
public static async Task<int> Main() { }
public static async Task Main(string[] args) { }
public static async Task<int> Main(string[] args) { }
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-line
Main() and command-line arguments - C#
Learn about Main() and command-line arguments. The 'Main' method is the entry point of an executable program.
Steadhaven
SteadhavenOP10mo ago
lol wow
Pobiega
Pobiega10mo ago
99% of the time void is fine - int is useful if you want to explicitly control the errorcode
Steadhaven
SteadhavenOP10mo ago
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)
Pobiega
Pobiega10mo ago
useful for stuff like kubernetes its the same really
Steadhaven
SteadhavenOP10mo ago
Yeah ok then I won't have to worry, all of my code so far is just using main method returning void
Want results from more Discord servers?
Add your server