73

I have installed the preview version of Microsoft's new code editor "Visual Studio Code". It seems quite a nice tool!

The introduction mentions you can program c# with it, but the setup document does not mention how to actually compile c# files.

You can define "mono" as a type in the "launch.json" file, but that does not do anything yet. Pressing F5 results in: "make sure to select a configuration from the launch dropdown"...

Also, intellisense is not working for c#? How do you set the path to any included frameworks?

Launch.json:

"configurations": [
        {
            // Name of configuration; appears in the launch configuration drop down menu.
            "name": "Cars.exe",
            // Type of configuration. Possible values: "node", "mono".
            "type": "mono",
            // Workspace relative or absolute path to the program.
            "program": "cars.exe",
        }, 
        {
            "type": "mono",
        }
3
  • 1
    I'm interested in answer too.
    – vvvsg
    Commented May 10, 2015 at 10:05
  • 1
    did you read this? code.visualstudio.com/Docs/languages
    – vvvsg
    Commented May 10, 2015 at 10:09
  • yes... C# seems to be removed entirely from the docs?
    – Kokodoko
    Commented Aug 21, 2015 at 10:16

5 Answers 5

65

Since no one else said it, the short-cut to compile (build) a C# app in Visual Studio Code (VSCode) is SHIFT+CTRL+B.

If you want to see the build errors (because they don't pop-up by default), the shortcut is SHIFT+CTRL+M.

(I know this question was asking for more than just the build shortcut. But I wanted to answer the question in the title, which wasn't directly answered by other answers/comments.)

2
  • 1
    Thanks :) I know the shortcut, but that just calls the configuration in tasks.json does it not? I'm still not clear on how to include a C# compile task, so I just installed Visual Code for Mac...
    – Kokodoko
    Commented Jan 24, 2017 at 17:27
  • 1
    Did you install the C# extension? You can install it by going to the Extensions tab on the left and searching for "C# for Visual Studio Code (powered by OmniSharp)". For some reason VS Code doesn't ship with a C# compiler, so you have add an extension to do it.
    – Brandon S
    Commented Jan 25, 2017 at 19:27
18

Intellisense does work for C# 6, and it's great.

For running console apps you should set up some additional tools:

  • ASP.NET 5; in Powershell: &{$Branch='dev';iex ((new-object net.webclient).DownloadString('https://raw.githubusercontent.com/aspnet/Home/dev/dnvminstall.ps1'))}
  • Node.js including package manager npm.
  • The rest of required tools including Yeoman yo: npm install -g yo grunt-cli generator-aspnet bower
  • You should also invoke .NET Version Manager: c:\Users\Username\.dnx\bin\dnvm.cmd upgrade -u

Then you can use yo as wizard for Console Application: yo aspnet Choose name and project type. After that go to created folder cd ./MyNewConsoleApp/ and run dnu restore

To execute your program just type >run in Command Palette (Ctrl+Shift+P), or execute dnx . run in shell from the directory of your project.

4
  • 11
    Thanks for the explanation - although I'm not sure I have any idea what's going on here :) In any case it's a far cry from the "outstanding experience" as MS describes it... Perhaps this is because the tool is yet new and the docs are incomplete. For now it seems that VS Code is primarily useful for web development.
    – Kokodoko
    Commented May 12, 2015 at 10:24
  • > I'm not sure I have any idea what's going on here You should install ASP.NET 5, because you need .NET Core environment. Then you need well formatted project.json. The recommended way of it's generating is using yeoman project generator — here comes all this “web-like” stuff: npm for package installing, yo, etc.
    – Viktor Pti
    Commented May 13, 2015 at 8:26
  • 1
    Ah, what you're saying is that you should prepare your environment to compile c# files from the command line directly. If you get that working, then you can add the CLI command to VS Code and it will work there too.
    – Kokodoko
    Commented Feb 4, 2016 at 13:41
  • What about on Linux? Commented Jan 9, 2019 at 10:47
6

SHIFT+CTRL+B should work

However sometimes an issue can happen in a locked down non-adminstrator evironment:

If you open an existing C# application from the folder you should have a .sln (solution file) etc..

Commonly you can get these message in VS Code

Downloading package 'OmniSharp (.NET 4.6 / x64)' (19343 KB) .................... Done!
Downloading package '.NET Core Debugger (Windows / x64)' (39827 KB) .................... Done!

Installing package 'OmniSharp (.NET 4.6 / x64)'
Installing package '.NET Core Debugger (Windows / x64)'

Finished
Failed to spawn 'dotnet --info'  //this is a possible issue

To which then you will be asked to install .NET CLI tools

If impossible to get SDK installed with no admin privilege - then use other solution.

5

Install the extension "Code Runner". Check if you can compile your program with csc (ex.: csc hello.cs). The command csc is shipped with Mono. Then add this to your VS Code user settings:

"code-runner.executorMap": {
        "csharp": "echo '# calling mono\n' && cd $dir && csc /nologo $fileName && mono $dir$fileNameWithoutExt.exe",
        // "csharp": "echo '# calling dotnet run\n' && dotnet run"
    }

Open your C# file and use the execution key of Code Runner.

Edit: also added dotnet run, so you can choose how you want to execute your program: with Mono, or with dotnet. If you choose dotnet, then first create the project (dotnet new console, dotnet restore).

1
  • Thanks! I'm going to try it. Would external libraries such as monogame also compile? This seems a lot easier than using Visual Studio or Xamarin.
    – Kokodoko
    Commented Jul 4, 2017 at 10:03
3

To Run a C# Project in VS Code Terminal

  1. Install CodeRunner Extension in your VS Code (Extension ID: formulahendry.code-runner)
  2. Go to Settings and open settings.json
  3. Type in code-runner.executorMap
  4. Find "csharp": "scriptcs"
  5. Replace it with this "csharp": "cd $dir && dotnet run $fileName"

Your project should Run in VS Code Terminal once you press the run button or ALT + Shift + N

Not the answer you're looking for? Browse other questions tagged or ask your own question.