How YOU can get started with .NET Core and C# in VS Code
Tags: dotnet, webdev, tutorial, beginners
Published: 2019-07-17 21:02:02

---
title: How YOU can get started with .NET Core and C# in VS Code
published: true
description: A tutorial on how to get into .NET Core development. Shows solution, libraries, NuGet, testing, debugging and some nice extensions
tags: dotnet, webdev, tutorial, beginners
cover_image: https://thepracticaldev.s3.amazonaws.com/i/xn2aev7d7z7hnsfqcf1t.jpg
canonical_url: https://softchris.github.io/pages/dotnet-core.html
---

Follow me on [Twitter](https://twitter.com/chris_noring), happy to take your suggestions on topics or improvements /Chris

> This article covers your first steps using .NET Core, the cross-platform version of .NET. We will be creating a solution, a library, a console app and also a test project. Of course, we will be taught how to execute our tests, all this from the terminal

.NET has been around since 2001 and a lot of developers have cut their teeth on using Visual Basic .NET or C# with Visual Studio. They were using Windows at the time. Times have changed, cross-platform is a must nowadays and .NET as well as Visual Studio exists on both Windows, Linux, and Mac.

In this article we will cover:

- **Installing .NET Core** and some nice extensions like C# 
- **Scaffold** a solution
- **Add a class library** and add it to the solution
- **Add a test project** and run our tests
- **Add a Console program** and use our library
- **Improve our IDE** with vscode-solution-explorer extension

what commands we were taught and what to look at next like ASP, Docker, Cloud

## Resources

- [Host a .NET Core app](https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/?view=aspnetcore-3.0)
There are many ways to host a .NET Core app, Windows, Azure etc.

- [Publish you .NET Core app to the Cloud](https://docs.microsoft.com/en-us/aspnet/core/tutorials/publish-to-azure-webapp-using-vscode?view=aspnetcore-3.0)
Let's learn how to publish our .NET Core app to the Cloud and Azure.

- [Sign up for a free Azure account](https://azure.microsoft.com/free/?wt.mc_id=devto-blog-chnoring)
To be able to use Azure you will need a free Azure account

- [Download .NET Core](https://dotnet.microsoft.com/download)

- [The tutorial this article is based on](https://docs.microsoft.com/en-us/dotnet/core/tutorials/using-on-macos?wt.mc_id=devto-blog-chnoring)

- [Hello world tutorial - console app](https://docs.microsoft.com/en-gb/dotnet/core/tutorials/with-visual-studio-code?wt.mc_id=devto-blog-chnoring)
This shows a simple Hello World console application

- [Containerize a .NET Core App](https://docs.microsoft.com/en-us/dotnet/core/docker/build-container?wt.mc_id=devto-blog-chnoring)
This explains how to Dockerize your app, how to author the Dockerfile etc.

- [Unit testing in .NET Core](https://docs.microsoft.com/en-us/dotnet/core/testing/?wt.mc_id=devto-blog-chnoring)
There is more than one choice for unit testing

- [dotnet CLI - all commands](https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-new?tabs=netcore22&wt.mc_id=devto-blog-chnoring)
There are so many commands for the `dotnet` tool. Did you know that you can scaffold a .NET project with a React frontend for example?

- [Create a new web app with ASP and .NET Core](https://docs.microsoft.com/en-us/aspnet/core/getting-started/?view=aspnetcore-2.2&tabs=macos&wt.mc_id=devto-blog-chnoring)
Great tutorial for creating your first Web app in .NET Core

## Install and Set up

The first thing you want to do is to install .NET Core. Head over to this page:

> [https://www.microsoft.com/net/download/core](https://www.microsoft.com/net/download/core?wt.mc_id=devto-blog-chnoring)

Once it's installed you should have access to a command-line tool you can invoke in the terminal called `dotnet`. You can easily see what commands are available by typing:

```
dotnet --help
```

![](https://thepracticaldev.s3.amazonaws.com/i/n0hmptbs2k13x28phjr0.png)

Quite a few commands as you can see above. We will explore some of these commands for creating solutions and projects.

Because we will be working with C# and VS Code I recommend downloading VS Code

> https://code.visualstudio.com/download

Once you got VS Code on your system go and install the C# extension. Type `C#`, it should look like so:

![](https://thepracticaldev.s3.amazonaws.com/i/jwu612zbdx2t4yt35x38.png)

Good, now we are set up and ready code :)

## Creating a solution

The first we will do is to create a solution. A solution is something we use to keep track of all related projects. As an old .NET developer you might be used to do this via Visual Studio but we will actually do this via the terminal.

We will take the following steps:

1. Create a directory for our solution
2. Invoke the command generating a solution

First things first, let's create a directory:

``` 
mkdir app
cd app
```

You can name your directory anything you want, `app` was just an example.

Next, let's create a solution. We do this with the following command:

```
dotnet new sln
```

`sln` is short for `solution`, this will generate a new solution. Because we are standing in the `app` directory this generates an `app.sln` file. 

## Create a library
Next up we will generate a library. Now a library is not an executable program but more a set of files we include in other projects. 

To create a library we will need to type the following in the terminal (we are still in the `app` directory):

```
dotnet new classlib -o library
```

Above we can see that our general command is `dotnet new`, followed by the type, which in this case is `classlib`, then we use the flag `-o` and the last argument is the name of the project.

Let's have a look at what we got:

![](https://thepracticaldev.s3.amazonaws.com/i/hzoxldg09xxfhoe9xswv.png)

We see that the directory `library` was created. We also see that we got the file `Class1.cs`, the project file `library.csproj` and a directory `obj`. At this point, lets switch to VS Code and look at our files. Looking att `Class1.cs`, it looks like this:

```csharp

// Class1.cs

using System;

namespace library
{
  public class Class1
  {
  }
}
```

It doesn't do much but it's valid C# code. 

Let's finish this section by adding to the solution:

```
dotnet sln add library/library.csproj
```

This adds the following entry to `app.sln`:

```
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "library", "library\library.csproj", "{7F30581E-9BCD-46FB-B7A5-FC2A85DE1429}"
```

For now, we are not going to spend so much time thinking about this other than we have a solution that knows about our library. 

## Updating our library

Ok, we need to add some code to our library project. We will do the following:

1. **Download** a NuGet package from a NuGet repository
2. **Reference** our NuGet package in our library code
3. **Build** our code

### Download and use a package from the NuGet repository

> https://www.nuget.org/

Is a big (160k + libraries) repository where all NuGet libraries live. Anything that lives here can be downloaded by a simple command. For this part, we will grab a specific library called `Newtonsoft.Json`. This will allow us to parse JSON back and forth. To make this library part of our solution we can type:

```
dotnet add library package Newtonsoft.Json
```

The above means we are adding the package `Newtonsoft.Json` to the project `library`. The command should be read like this:

```
dotnet add [what project to add it to] package [name of package at nuget.org]
```

Sometimes you might be working on an existing project or you might have grabbed the latest changes. Regardless of which, you might be missing some packages that your project or solution needs to run properly. At that point, you can run the command

```
dotnet restore
```

This will grab the packages specified as package references in your projects `csproj` file.

### Update our library code

In your library project, change the file `Class1.cs` to `Thing.cs` and add the following code to it:

```csharp
// Thing.cs

using static Newtonsoft.Json.JsonConvert;

namespace Library
{
  public class Thing
  {
    public int Get(int left, int right) =>
        DeserializeObject<int>($"{left + right}");
  }
}
```

### Build

Next up we need to compile our code. This will, if successful, generate a so-called `.dll` file. This is the format used by .NET for libraries and stands for dynamic link library.

So let's compile our code with the command:

```
dotnet build
```

![](https://thepracticaldev.s3.amazonaws.com/i/3w4i6wng5yvs4tahy2h3.png)

As indicated in the above image, we get a `library.dll` 

Success!

## Creating a test library

Next up we will create a test project. All codes deserve some level of testing and what better way to start than having tests set up early on. To create a test project we need to type the following:

```
dotnet new xunit -o test-library
```

This will create a test project of type `xunit` with the name `test-library`.

### Adding references

Let's add our project to the solution file as well:

```
dotnet sln add test-library/test-library.csproj
```

The idea here is to test functionality found in our project called `library`. For this to be possible we need to add a reference to `library` in our `test-library` project, like so:

```
dotnet add test-library/test-library.csproj reference library/library.csproj
```

### Writing a test

When we created our `test-library` we got a file called `UnitTest1.cs` created as well. Let's have a look at this file:

```csharp

//UnitTest.cs

using System;
using Xunit;

namespace test_library
{
    public class UnitTest1
    {
        [Fact]
        public void Test1()
        {

        }
    }
}
```

We use the decorator `[Fact]` to indicate that this is a test method that needs to be evaluated and ran. Let's add some code next to `UnitTest.cs`:

```csharp

//UnitTest.cs

using Library;
using Xunit;

namespace TestApp
{
    public class LibraryTests
    {
        [Fact]
        public void TestThing() {
            Assert.NotEqual(42, new Thing().Get(19, 23));
        }
    }
}
```

### Running our test/s

We are now ready to run our test and inspect the outcome. 

To run the tests, type:

```
dotnet test test-library/test-library.csproj
```

We use the command `dotnet test` and the last argument is where to find the project file for the test project. So what do we get?

![](https://thepracticaldev.s3.amazonaws.com/i/3yh4fx30ql8yfwr43tep.png)

We can see from the above image that our test run failed. It clearly indicated `[Fail]`. Let's look at the WHY. *Expected the result to be NOT 42*. Hmm, let's check the implementation in `Thing.cs` in our `library` project:

```csharp
public int Get(int left, int right) =>
        DeserializeObject<int>($"{left + right}");
```

We cleary just take `2` input arguments and sum them up. What about the test in `UnitTest1.cs`?

```csharp
public void TestThing()
{
  Assert.NotEqual(42, new Thing().Get(19, 23));
}
```
`19` + `23` to be NOT equal to 42. Wait a minute... It should be `Equal`. Let's change that:

```csharp
public void TestThing()
{
  Assert.Equal(42, new Thing().Get(19, 23));
}
```

and let's rerun the test:

```
dotnet test test-library/test-library.csproj
```

Let's inspect the results and....

![](https://thepracticaldev.s3.amazonaws.com/i/sariv0g1pum06ft3knx5.png)

This time around the tests is succeeding.

Success! :)


## Console app

Next thing we are going to do is to create a Console App. Why do you ask? Well, we want to show the whole idea of creating a reusable library that we can drop in anywhere, from test projects to console projects. 

Let's generate our console project like so:

```
dotnet new console -o console-app
```

This gives us a runnable project with a file `Program.cs` that serves as the entry point. Technically the entry point is a method called `Main()`:

```csharp

// Program.cs

using System;

namespace console_app
{
  class Program
  {
    static void Main(string[] args)
    {
        Console.WriteLine("Hello World!");
    }
  }
}
```

Let's add this project to the solution file, like so: 

```
dotnet sln add console-app/console-app.csproj
```

### Adding and using our library project

Next order of business is to start using our `library` project and make it part of our `console-app` project. So we add the dependency like so:

```
dotnet add console-app/console-app.csproj reference library/library.csproj
```

Read the above as:

```
dotnet add [target project] reference [destination project]
```

Next up we need to change the code of our `Program.cs` file to the following:

```csharp

// Program.cs

using System;
using Library;

namespace console_app
{
  class Program
  {
    static void Main(string[] args)
    {
        Console.WriteLine($"The answer is {new Thing().Get(19, 23)}");
    }
  }
}

```

Let's run the app next

```
dotnet run -p console-app/console-app.csproj
```

![](https://thepracticaldev.s3.amazonaws.com/i/yn68cztpmihshwfcwct8.png)

We got ourselves a running app, using the code from the `library` project.

Awesome :)

## Debugging
Debugging. That's one of the great things about using Visual Studio. It was and is outstanding. Personally, I think it's second to none, so what's the situation in VS Code?

Well, let's see how we debug and then reflect on how well it does it.

Now VS Code will ask you for adding a needed resource for debugging if you open a solution missing it. So how do I know what's missing? Well you should have a `.vscode` directory looking like this:

![](https://thepracticaldev.s3.amazonaws.com/i/guwfqzaipnj8kubzxcc1.png)

> That's it?

Well, you might try to select `Debug/Start Debugging` from the menu and you will notice it's running the `test-library` if you look at the end of the debug window

![](https://thepracticaldev.s3.amazonaws.com/i/520oxg1fdib0g1x0laqg.png)

Now, you probably want to change that to actually run the `console-app` project when debugging. To make that happen we need to go into `launch.json` under the directory `.vscode`. Find the entry looking something like this:

```json
{
  "name": ".NET Core Launch (console)",
  "type": "coreclr",
  "request": "launch",
  "preLaunchTask": "build",
  // If you have changed target frameworks, make sure to update the program path.
  "program": "${workspaceFolder}/test-library/bin/Debug/netcoreapp2.2/test-library.dll",
  "args": [],
  "cwd": "${workspaceFolder}/test-library",
  // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
  "console": "internalConsole",
  "stopAtEntry": false
}
```

We need to change the property `"program"` to point to our console app and also the `"cwd"`.

It should now look like the below in `launch.json`

![](https://thepracticaldev.s3.amazonaws.com/i/4vsih00rmnz7a8pwuq5y.png)

Then select menu option `Debugging/Start Debugging` and we should see the below

![](https://thepracticaldev.s3.amazonaws.com/i/kxy4pivxqiwyausi91kh.png)

There we have it, boys and girls, working debugging :)

So, the experience, well the first thing that happened was that I just noticed that when I changed the code in `Program.cs` my breakpoint was not hit properly. This had an explanation. Looking at our entry in `launch.json` we see there is a property `"preLaunchTask": "build"` that points out a task that should run before our debug. This task resides in `tasks.json`. Heading over to that file we noticed that it looked like this:

```json
{
  "label": "build",
  "command": "dotnet",
  "type": "process",
  "args": [
      "build",
      "${workspaceFolder}/test-library/test-library.csproj"
  ],
  "problemMatcher": "$tsc"
}
```

YES, you are correct we weren't building the correct project before running the debug again. The way to fix this was to either run 

```
dotnet build
```

in the terminal OR, fix this task and have it point to our `console-app`, like so:

![](https://thepracticaldev.s3.amazonaws.com/i/8zzn0j1wrmkf1fmgd9es.png)

If you were to debug again everything works. Now you know a little bit more how things work in VS Code. 

If I were to have an opinion here I would like to have this abstracted away from me as a user. I'd rather want a plugin doing this for me. At this point, I wish for a solution explorer, like the one I have in Visual Studio.  

## Improve IDE

If you are like me with more 10+ years on the .NET platform, you are looking for ways to make this into Visual Studio as much as possible. You, of course, welcome new ways of doing things like using the terminal but it feels like you want a UI. Maybe I should use the actual Visual Studio then? 

Well, there is a plugin that fixes this. It's called `vscode-solution-explorer`. If you install it your project will suddenly look like this:

![](https://thepracticaldev.s3.amazonaws.com/i/xyitqvyr6y22twnq1zh7.png)

and right-clicking on a project will give you a menu like this:

![](https://thepracticaldev.s3.amazonaws.com/i/uptrdog1uxx3hbizxnc6.png)

My spontaneous reaction is :)

![](https://thepracticaldev.s3.amazonaws.com/i/gognv2gbb19p32nis3j6.gif)

This is not an official extension by Microsoft but it's very nice and well worth a try if you like me enjoy terminal commands but sometimes just want a UI.


## Summary

Ok, let's try to sum this up. We have gotten quite the education on .NET Core. We were taught to use `dotnet` core to do everything from scaffolding different types of project to creating solutions, adding references, building our code to running tests. Quite versatile.

Lastly, we also showed a plugin that offers an alternate approach to all our terminal commands. Of course, under the hood, it's running our terminal commands, so there is definitely value to learn the ways of the CLI. :)