C
C#2y ago
sin.sineer

❔ 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
Saber
Saber2y ago
you can, only one can have top level statements
sin.sineer
sin.sineerOP2y ago
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 distilldowo
Saber
Saber2y ago
again, you can have multiple files in one folder, but only a single with top level statements
sin.sineer
sin.sineerOP2y ago
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
mtreit
mtreit2y ago
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.
Angius
Angius2y ago
You can have
// Program.cs
Stuff.Do();
// Program.cs
Stuff.Do();
// Stuff.cs
public class Stuff
{
public static void Do(){}
}
// Stuff.cs
public class Stuff
{
public static void Do(){}
}
but you cannot have
// Program.cs
Stuff.Do();
// Program.cs
Stuff.Do();
// Stuff.cs
static void Do(){}
// Stuff.cs
static void Do(){}
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
canton7
canton72y ago
(top level statements are supposed to simplify things for beginners, but they really don't. I recommend avoiding them)
Angius
Angius2y ago
They very much do make things easier They remove a bunch of useless boilerplate
mtreit
mtreit2y ago
And caused much confusion to beginners as a result As this question demonstrates.
sin.sineer
sin.sineerOP2y ago
I thank all of you guys, you really helped me a lot🖖
Accord
Accord2y ago
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.
Want results from more Discord servers?
Add your server