❔ Only one compilation unit can have top-level statements. [C# start]

using VS code, just created new file apart from program.cs and got this error
17 Replies
cap5lut
cap5lut2y ago
then, dont use top level statements there, but normal classes
public class AClassInYourOtherFile
{
public static void DoSomething()
{
// your code for this method
}
}
public class AClassInYourOtherFile
{
public static void DoSomething()
{
// your code for this method
}
}
for example if ur Program.cs looks like this for example:
using System;

Console.WriteLine("hello world");
using System;

Console.WriteLine("hello world");
its nothing different than that it automatically will be converted to
using System;

public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("hello world");
}
}
using System;

public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("hello world");
}
}
in the end. top level statements are to get rid of that boiler plate code also for beginners i would recommend using Visual Studio, not Visual Studio Code if possible
𝔎𝔦𝔰𝔰_𝔐𝔶_𝔄𝔵𝔢
im so confused, sorry Im so new to C# my code doesnt have "using system:"
Thinker
Thinker2y ago
In every C# project, there is one file that will be run at the start of your program - Program.cs. You cannot have multiple of these files, there can only be one.
𝔎𝔦𝔰𝔰_𝔐𝔶_𝔄𝔵𝔢
ok so what if i want to write a new code but want to keep my old one
Thinker
Thinker2y ago
You create a new project
𝔎𝔦𝔰𝔰_𝔐𝔶_𝔄𝔵𝔢
how do u do that on vs code?
Thinker
Thinker2y ago
Same way you created your current project dotnet new console presumably
𝔎𝔦𝔰𝔰_𝔐𝔶_𝔄𝔵𝔢
ok but when i did that it just runs my original project, not my new one @thinker227 what should i do? @cap5lut ???
Thinker
Thinker2y ago
Go into your new project in the terminal and run dotnet run
𝔎𝔦𝔰𝔰_𝔐𝔶_𝔄𝔵𝔢
what if i want to run it in terminal? does that not work?
Thinker
Thinker2y ago
It does
𝔎𝔦𝔰𝔰_𝔐𝔶_𝔄𝔵𝔢
in terminal on my mac as in externally? whatever, ill just run in app
Thinker
Thinker2y ago
Well, any terminal will work
𝔎𝔦𝔰𝔰_𝔐𝔶_𝔄𝔵𝔢
ok its fine i think I was running it wrong can i ask u if I have any more questions as I am very new to C# and coding in generall and you seem like you know what your doing
Thinker
Thinker2y ago
You can always ask in #help or #help-0 catlurk
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