scriptcs always giving an error saying that I should use System library to terminate the program

526 Views Asked by At

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!");
        }
    }
}
1

There are 1 best solutions below

1
On

You are using scriptcs to run your cs program. It looks like you are trying to create a console application, but running it as a script instead (dotnet run is used to run c# apps). This could be caused opening only the c# file in VS Code. Scripts are powerful, but they are not the same thing as c# projects. Using c# inside a project is probably what you are doing in Visual Studio, which is why it works there (and why it allows you to use namespaces). To open the project in VS Code, you need to open either the solution file (.sln), or the whole folder that contains your project files.

You can choose to either write code for a c# script and follow those rules, or write code for a c# project of some type (a console app looks like the type of project you would want). Your issue is that you are mixing the two.