Visual Studio Code run individual .cs files

12,577

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 ...

Share:
12,577
raghzz
Author by

raghzz

Updated on June 09, 2022

Comments

  • raghzz
    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

    link

    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
    raghzz about 6 years
    I 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
    Alsein about 6 years
    because 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
    Alsein about 6 years
    as 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
    Fildor about 6 years
    "But this way is highly deprecated for any further learning or development" - citation needed.
  • Alsein
    Alsein about 6 years
    there are many unit test technologies for it, why do you need to compile a single class file
  • Kir
    Kir about 3 years
    OP'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.