Getting Started With C# on Visual Studio Code

Adebiyi Adedotun
7 min readJul 14, 2018

--

If you’re going to write any serious code for Windows Development, then Visual Studio remains your hands-down Integrated Development Environment (IDE). The Microsoft Documentation page is also your go-to area.

Screenshot of docs.microsoft.com

You should understand: VSCode will only allow you write simple console applications that run in the terminal, or server services like ASP.NET. No room for GUI Apps with Universal Windows Platform or Cross-Platform Apps with Xamarin e.t.c.

What’s Xamarin

The Steps:

Step 1: Visual Studio Code

First, if you don’t have Visual Studio Code, you should download it here: visual studio code You can choose the version for your Operating System in the green drop-down button on the home page. There’s a click-through installation guide.

Visual Studio Code Website ScreenShot

Step 2: .NET Core

You also need .NET core. .NET core is the Operating System Independent Version of the .NET framework. .NET Core is light, cross-platform and fast. Get it at .NET Core SDK. It also has a click-through installation guide. To verify it’s installed after it’s done, run your Operating System’s terminal and run the command dotnet --version For me, it’s 2.1.3

dotNET core website screenshot

Detour:

There are a lot of things, .NET core can do for you. To check a list of possibilities, open up your terminal, type in the command dotnet new

dotnet Templates List

Step 3: Install Omnisharp

Omnisharp is a VSCode extension that tells VSCode about C#.

Install it by opening the extensions tab to the left of VSCode or go to view then extensions Type in Omnisharp in the search bar, then hit install. After installation, click the reload button to refresh VSCode.

VSCode Omnishap Extension page

Step 4: Create a folder

On the home-screen of you Operating System, create a new folder — name it whatever, I named mine welcome dotnet

Step 5: Fire up VSCode

Launch VSCode then on the top-left corner, click on file then from the dropdown, click on open folder The folder we’ll be opening is the folder created in step 4. From the dialog, locate your desktop directory then the created folder — welcome dotnet

VSCODE with an empty folder

The folder is blank now but to the left is the name of my folder welcome dotnet good. We are almost there.

Step 6: Open your VSCode built-in terminal

Launch the terminal by clicking view on the top navigation list of VSCode. Then from the dropdown, click integrated terminal. For more of what VSCode can do, check out: VSCode Can Do That

VSCode with Integrated Terminal

Step 7: Create a new Console App

From the terminal, run the command dotnet new console -n HelloWorld

With the command, you’re instructing dotnet core to create a new console app with the -n (name) HelloWorld

VSCode with a new dotnet console app

Step 8: Run the App

From the terminal, navigate to the HelloWorld app directory/folder. Use the command cd HelloWorld i.e Change Directory to the folder HelloWorld Then run the the app with the command dotnet run then wait.

Running a console app for VSCode

And voila, it ran.

But wait, there’s more.

That’s all there is to run a dotnet app. C#, F#…. As time goes on, you’ll want to have more apps in the same directory and you’ll need some config. But first, open up your HelloWorld folder from VScode folder list to the left, then open the file program.cs There is your C# code, and your Hello World output in the terminal.

Going Further With Expectations

From the Visual Studio experience, you sort of expect a lot of things from the C# compiler — Roslyn, and you just can not take less. Some of the features include:

  1. Intellisense
  2. Code Completion
  3. Code Refactoring
  4. Debugging

The first time you open a brand new folder with a C# file, VSCode should automatically prompt you to add a required assetsat the bottom-right of VSCode. If it does, go ahead and click yes. In the listing where you opened program.cs you’ll find a .vscode folder

If it doesn’t or you — for some reason, didn’t click yes — go ahead and create the folder .vscode and in it, the files launch.json and tasks.json For the tasks.json and launch.json file, add the code:

// tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/*/*.csproj"
],
"problemMatcher": "$msCompile"
}
]}

And for launch.json

{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/HelloWorld/bin/Debug/netcoreapp2.1/*.dll",
"args": [],
"cwd": "${workspaceFolder}/*",
"console": "internalConsole",
"stopAtEntry": false,
"internalConsoleOptions": "openOnSessionStart"
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
},
]
}
VSCode with Intellisense.

Immediately, you begin to notice something. From the last image above, is the 0 references in certain regions of my code. That’s the C# compiler — Roslyn, proactive and ready to lint and help you as you write your code. Because C# is a strongly typed language, you sort of need some way to know when you make mistakes, or when you need to do simple stuffs like check all references to a method or class if available. For now, that will all work — it does for me, it should for you too.

The .vscode file

What VSCode really does when it added the two files: launch.json and tasks.json is quite nice, but immediately you create another console app, it doesn’t recognize it. The problem here is quite simple. Let’s see what’s in these two files first.

VSCode with required assets to run C#

To the left are the two files tasks.json and launch.json To the right is the program.cs from the folder HelloWorld You’d notice that certain lines in the left files have the HelloWorld name in them. All these just means your HelloWorld folder is been targeted exclusively. What happens when we create another console app? I’ll do that from the terminal with the command dotnet new console -n HelloDotnet in the Welcome Dotnet folder on my desktop from step 4.

But I don’t get all the compiler goodies anymore. And the only way to know when I go wrong is when I run my code. Not nice. From the image above, my HelloWorld file is been targeted because my tasks.json and launch.json specifies that. Let’s change that to target any file added to the folder. Quite simple-in the launch.json and tasks.json , change any occurrence of HelloWorld to an asteriks * This — in Regular Expression-is a wild card meaning anything. So any file(s) that can be targeted will be targeted.

Then, reload VSCode. Use the command ctrl+shift+P or the cmd+shift+p if you’re on a Mac, or just close and then open VSCode again. Then keep coding, It will all work fine. It does for me. My two files now have error checking and intellisense and all those goodies.

C# is fun and great. There are many reasons while you should learn it. And for all it’s worth, if you still need something serious with windows development, you still need Visual Studio — the best of them all.

There’s more to the launch.json and tasks.jsonLearn about them here:

  1. github omnisharp
  2. debugger console

P.S There’s a video tutorial:

Have fun coding! 😉

--

--