First of all, I'm new to C# (I mean very new, I learned it yesterday.). I'm able to compile and run my code in Visual Studio 2019. And as Visual Studio 2019 is working slow on my computer, I decided to install C# extension to VS Code. But it doesn't including an option to compile & run the code. So I installed "Code Runner" extension to VS Code. (Finally C++ and C compiling is working after 1 hours of trying to find G++, install it and add to path) but C# is still not working. When I press the "run" icon on the toolbar (top of VS Code), it simply runs this on PowerShell:
scriptcs "c:\Users\ymzym\OneDrive\C# Projects\GitHub\Hello-World\Hello_World_C#.cs"
and gives me this error after 5 seconds:
ERROR: Script compilation failed. [CompilationErrorException] error CS7088: Invalid 'Usings' value: 'System // 'System' library is required for terminating the program in the 'Exit' function.'.
I tried everything according to error message. Also I already impored System library like using System;
, I tried removing System.Windows.Forms.Application.ExitThread();
from my code. I tried writing the whole program from beggining but no change. The more strange thing is only scriptcs cannot compile. Visual Studio 2019 can.
And the funny thing is, when I replace the whole code with a basic Hello World program, it gives me another error saying:
ERROR: Script compilation failed. [CompilationErrorException] c:\Users\ymzym\OneDrive\C# Projects\GitHub\Hello-World\Hello_World_C#.cs(1,1): error CS7021: You cannot declare namespace in script code
Here is my code:
using System;
namespace HelloWorld {
class Program {
static void Greeting() {
string version = "0.1.0";
string build = "Build 01";
Console.WriteLine("███████████████████████████████████████████████████████");
Console.WriteLine("███████████▓▒░ Hello World in C# (CSharp) ░▒▓██████████");
Console.WriteLine("███████████████████████████████████████████████████████");
Console.WriteLine($"█████████████▓▒░ Version {version} {build} ░▒▓████████████");
Console.WriteLine("███████████████████████████████████████████████████████");
}
static void Question() {
Console.WriteLine("█▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀█");
Console.WriteLine("█ [1] Hello World █");
Console.WriteLine("█ [2] Greeting █");
Console.WriteLine("█ [3] Exit █");
Console.WriteLine("█▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄█");
Console.Write("Your choice: ");
}
static void Exit() {
System.Windows.Forms.Application.ExitThread();
}
static void Main(string[] args) {
string version = "0.1.0";
string build = "Build 01";
Console.Title = $"Hello World in C# (Csharp) - Version {version} {build}";
Greeting();
while (true) {
Question();
try {
string choice = Console.ReadLine();
if (choice == "1") {
Console.WriteLine("Hello World!");
}
else if (choice == "2") {
Console.Write("Type your name: ");
string name = Console.ReadLine();
Console.WriteLine($"Hello, {name}");
}
else if (choice == "3") {
Exit();
}
else {
Console.WriteLine($"ERROR: There is no function defined for '{choice}', please type 1, 2 or 3 which are showed above.");
}
}
catch (Exception) {
Console.WriteLine("ERROR: Your choice must be a number. Numbers are shown above.");
}
}
}
}
}
And this is the simple Hello World program that still causes error:
namespace HelloWorld {
class Hello {
static void Main(string[] args) {
System.Console.WriteLine("Hello World!");
}
}
}