Only one compilation unit can have top-level statements. [Project]csharp(CS8802) [Answered]
I want to make my code cleaner by making different files. But when i make different files i get this error. Any help?
15 Replies
if i write this code in my other file than there is no problem
but i want different files to make my code cleaner
then use regular classes instead of top-level statements in the other files
uhh what? i'm new to c# soory
top-level statements are basically just there to remove some boiler plate
is just short for
oh ok
how can i use regular classes than?
C# only allows top level in Program.cs.
so if u would want to split up ur code for readability u would have to write more methods/classes.
it highly depends on the nature of ur application how to split it up semantically
You have to put your code inside classes.
yeah yeah i understand
is there an externsion is vscode to beautify my code
You can reformat using shit alt f
You should be good to go if you have the c# extension installed.
basically methods/classes are grouping some logic together, eg, one to read data from a website, another to process that data and a third to give some output.
ur Main method (or Program.cs' top level statements) would simply "wire" the method calls together into the actual program
oh yeah ok
thx
basically u could do something like
(these are ofc just rough examples)
also i would recommend to do some beginner tutorials first to get the basics down (ofc, we r here to help on arising questions/problems)
if ur question is answered now, please use
/close
to mark it as that
@EliasGPS ⤴️i just made another file with another class
and than i make an object in the main file and call my method
✅ This post has been marked as answered!