Create A .NET Core Development Environment Using Visual Studio Code - Part Two

In my previous post, I have talked about setting up a .NET Core development environment using Visual Studio Code. It had covered how to setup VS Code to run .NET Core Applications with the help of .NET CLI. Even though we were able to develop an application with VS Code, we were missing many things that most of the .NET developers are accustomed to their standard IDE - Visual Studio. One such feature is the Solution Explorer.

You can read the first part from the following link.
When .NET projects are being built, they are usually contained in solutions. Solutions are containers used by Visual Studio to organize one or more related projects; for example, a class library and a corresponding test project. When we open a solution in Visual Studio, it automatically loads all the projects that solution contains. Solution Explorer is the Visual Studio feature which helps to visualize our project solution structure in a tree view. This helps in easy navigation of the project files.

.NET Core Development Environment using Visual Studio Code
Figure: Solution Explorer in Visual Studio
In this article, I will explain how to bring the solution explorer functionality to our lightweight source code editor - Visual Studio Code.

.NET Core Solutions - The .NET CLI way

  • We shall create a simple calculator application which shall do the basic math operations. The application shall contain a C# class library project and a console application.

  • Create a new folder named SimpleCalculator and open that folder in Visual Studio Code.

  • Open the terminal in VS Code and enter the command dotnet new sln --name SimpleCalculator. This command will create an empty solution in the directory.

  • Let us create our class library first. Enter the command dotnet new classlib --name MathOperations. This will create a class library project named MathOperations in our directory.

  • Now we need to add the class library project we created to our solution. For this, we need to run the dotnet sln <SOLUTION_NAME> add <PROJECT> command where <SOLUTION NAME> is the name of the solution and <PROJECT> is the path of the .csproj file of the C# project which we are adding to our solution.

  • So to add our class library to our solution we shall run the command dotnet sln SimpleCalculator.sln add MathOperations\MathOperations.csproj.

  • Navigate to the class library directory MathOperations. Rename the Class1.cs class file to MathOperations.cs. Add a simple method for adding two numbers in the class.
    1. public static class MathOperations    
    2. {    
    3.     public static int Add(int num1, int num2) => num1 + num2;    
    4. }  
  • Let's add the console application in our project. Enter the command dotnet new console --name Calculator. This will create a console application project.

  • We need to add this console application to our solution. Enter the command dotnet sln SimpleCalculator.sln add Calculator\Calculator.csproj for that.

  • For accessing the methods we defined in our class library by our console application we need to add a reference of our class library project in our console application. We can use dotnet add reference command for that. Enter the command dotnet add Calculator\Calculator.csproj reference MathOperations\MathOperations.csproj for adding the reference.

  • Modify the `Program.cs` file to use our method from the class library.
    1. class Program    
    2. {    
    3.     static void Main(string[] args)    
    4.     {    
    5.          int num1 = 10;    
    6.          int num2 = 20;    
    7.          int sum = MathOperations.Add(num1, num2); // Method from class library    
    8.          Console.WriteLine($"{num1} + {num2} = {sum}");    
    9.          Console.ReadLine();    
    10.      }    
    11. }   
  • Navigate to the console application directory and execute the command dotnet run. We shall get the output 10 + 20 = 30 in the console. 
We had just created a .NET Core solution with a class library and a console application with .NET CLI. But for the developers who are used to Visual Studio and are less comfortable with command line tools, this approach is a letdown. Fortunately, a nice VS Code extension was developed to address this issue.

.NET Core Solutions - The Solution Explorer Way

Open Visual Studio Code and install the extension vscode-solution-explorer. This is an extension which will provide a solution explorer view for VS Code. Even though it is not a full feature equivalent to Solution Explorer in Visual Studio, this will provide most of the necessary options like displaying the solution and projects in a tree mode, providing context menus for tasks like adding and removing projects, adding and removing project references etc.

.NET Core Development Environment using Visual Studio Code
  • After installing this extension, a new pane named Solution Explorer will be displayed in the VS Code explorer sidebar.
.NET Core Development Environment using Visual Studio Code
  • Let us create an empty solution. Open the command palette (Ctrl + Shift + P) and enter the text solution. Choose the option "Create a new empty solution". VS Code will prompt to give the solution name. Let's give the same solution name `SimpleCalculator` as we gave earlier.

  • Now VS Code will create an empty solution with the name we provided. Behind the scenes, the extension we installed will execute the dotnet new sln command. You can see the empty solution in the Solution Explorer pane.

  • This extension will ask you to create a template folder See the below figure. If allowed it will add some templates in the .vscode/solution-explorer directory.
.NET Core Development Environment using Visual Studio Code
  • Now, let's add the class library and Console Application to our solution. Right click on the solution (in the Solution Explorer pane) and select Add new project option from the context menu. This will list the available project types provided by .NET CLI (See the figure below). Select the Class library option.
.NET Core Development Environment using Visual Studio Code
  • You will be asked which language you will be using. Select C# and the editor will prompt to enter the name for the project. Give the name MathOperations as we gave before. The class library is added to the solution.

  • Repeat the same steps and add the console application also. Remember to select Console application from the project templates.

  • Now we need to add the reference of our class library project in our console application. Right click on the console application project and select Add Reference option from the context menu. Since there are only two projects in the solution, the extension will automatically add the reference of the other project. If there are more than two projects we need to select the project from a list.

  • Now after adding the reference, make the necessary code changes in the application. You can refer to the previous example we did before.

  • Now right click on the console application project in the solution explorer tree and select the Run option from the context menu. You can see that .NET CLI will execute behind the scenes and run the application. The output will be displayed in the Output window as shown in below figure.
.NET Core Development Environment using Visual Studio Code

That's it. We have created a .NET Core application using the Solution Explorer extension of Visual Studio Code.


.NET CLI will not be a choice for most of the developers who are accustomed to the GUI way of doing things in Visual Studio. So in this post, I have mentioned about a great Visual Studio Code extension which provides the capabilities of the solution explorer of Visual Studio which can be used by the developers who do not like the CLI way of doing things. This extension is actually an abstraction over the .NET CLI i.e the actual CLI commands are being executed in the background by this extension. You can view the commands being executed in the output window of Visual Studio Code.

So we have just enhanced our awesome text editor to make development in .NET Core more interesting. To know more about this extension, check out the Github repository here.

Recommended Ebook

Printing in C# Made Easy

Download Now!
Similar Articles