❔ Only one compilation unit can have top level statements err simple explanation
when I create another cs file in the same folder I get this error, I just want to know how can I make 2 or more cs files in one folder.
is it even possible.?
edit: in vscode
11 Replies
you can, only one can have top level statements
but what happens when I have a big project that I need more files in one folder? do I need more files for big projects in one
again, you can have multiple files in one folder, but only a single with top level statements
I'm sorry I don't understand I'm very noob. yes I know I can have more than one file. but how to give the file I want a top-level statement.
how can I run a specific file I want
Your other files should contain type definitions (classes, enums, etc)...you don't "run" individual c# files. The compiled program has a single entry point, the Main method, where execution starts. Top level statements just hide the main method from you.
You can have
but you cannot have
One and only one file in the project, the
Program.cs
file can not have a class in it
The others must have a class, a struct, a record, an enum, or an interface(top level statements are supposed to simplify things for beginners, but they really don't. I recommend avoiding them)
They very much do make things easier
They remove a bunch of useless boilerplate
And caused much confusion to beginners as a result
As this question demonstrates.
I thank all of you guys, you really helped me a lot🖖
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.