Visual Studio Code run individual .cs files
Solution 1
dotnet command is just for .NET Core and you must do dotnet new console
command to generate a project to run it
if you are using .NET Framework and having the environment variables correctly set on your system, you can use csc Program.cs
command to individually compile a class file to an executable. But this way is highly deprecated for any further learning or development
Solution 2
For this i've always used this c# scripting engine : http://csscript.net,
very easy to implement and works very well and you can reference local dlls, Nuget packages or GAC assemblies.
It also has a Visual studio code plugin, so you can run with F5, debug ...
raghzz
Updated on June 09, 2022Comments
-
raghzz almost 2 years
Is there a way we can run individual .cs files in Visual Studio Code.
I have followed this link and runs fine but then I added Program2.cs and try to run using "dotnet run Program2.cs" but it failed for obvious reasons saying
Program2.cs(7,21): error CS0111: Type 'Program' already defines a member called 'Main' with the same parameter types [csharp.csproj]"
Is there a way to run individual .cs files. Or any way I can run individual .cs files that outputs to terminal/command prompt or console.
-
raghzz about 6 yearsI have created a .csproj using the link in the original post and now I have two .cs file with main in each of them. Is it possible to run those individual files even when it has main in both files. I just need to run individual files, more of files that prints output to command prompt.
-
Alsein about 6 yearsbecause if you use dotnet command to build and run the code, it ONLY accepts project for compiling, and you have 2 Mains in your project (project files is specified by folder in .NET Core, so as long as Program2.cs is in the folder, it is counted as a part of your project) and it cannot decide which to run.
-
Alsein about 6 yearsas i said, if you wanted to compile code with single cs file, you need to use 'csc' command which is a completely different .NET implement
-
Fildor about 6 years"But this way is highly deprecated for any further learning or development" - citation needed.
-
Alsein about 6 yearsthere are many unit test technologies for it, why do you need to compile a single class file
-
Kir about 3 yearsOP's question is about running individual .cs files like an executable/program. dotnet build = msbuild will ask for a project or solution file to build. It will not build program.cs without one.