Almost all .NET developers are accustomed to developing applications using the Visual Studio IDE from Microsoft. But this has a limitation - Visual Studio was a Windows application. This meant that you needed a Windows OS machine to develop .NET applications using Visual Studio. But with the introduction of .NET Core, which is both open source and cross-platform, Microsoft turned over a new leaf. .NET Core, a total rewrite of the .NET Framework came with a CLI tool, referred to as .NET CLI, can be used by developers who don't wish to use Visual Studio for developing .NET applications with the help of command line and just a text editor of their preference.
Speaking of text editors, I would like to mention that Microsoft has developed a cross-platform and open source text editor called Visual Studio Code. Visual Studio Code is so powerful and amazing that it provides IntelliSense, Debugging, and Git support built in. It is so extensible that if it is not providing something that you look for, you can find an extension that meets your need in their extensions library. If you didn't find it in their extensions library, you could just make one of your own and use it. Cool, right?
In this article, I will be talking about how to set up Visual Studio Code so that it can be used to develop applications using .NET Core.
Prerequisites
- Install .NET Core SDK. You can download the installer from here. When the installation is complete, open a command prompt or terminal and type dotnet --version command. You will be seeing something like 2.1.301 which is the .NET Core version you installed in your system. If you see the version number then .NET Core is correctly installed and ready to use.
- Install Visual Studio Code. You can download the installer from here. Depending upon your OS the installation process may vary. You can get the setting up VS Code instructions here.
- Install the C# extension for Visual Studio Code. For this bring up the Extensions view by clicking on the Extensions icon in the Activity Bar on the side of VS Code or use the keyboard shortcut Ctrl+Shift+X. Search for C# in the search box and install the extension from the list.
Now we are ready to develop .NET Core applications using Visual Studio Code. Let's look at an example.
For developing our first .NET Core application using Visual Studio Code we will use the .NET CLI tool.
- Create an empty folder. Name it DotNet Core Sample. Right click on that folder. From the displayed context menu, select Open with Code option. This will open the Visual Studio Code with a selected folder as your workspace.
- Use Ctrl+` (backtick character) shortcut to open the built-in terminal in Visual Studio Code.
- We will be creating a basic console application using the dotnet CLI. Type dotnet new console --name DotNetCoreSample in the terminal window and press Enter. The .NET CLI tool will create a console application with a Program.cs file and will restore the packages required for the project. See the figure below.
- Open the Program.cs file. When you open the file, installed C# extension will show a notification toast in the lower right corner of your editor offering to generate build and debug assets for your project. Click on the Yes button. This will create two configuration files in your source named `launch.json` and `tasks.json`. These files will help in building and debugging your applications using Visual Studio code.
- In the Program.cs file, change the Hello World string to My First .NET Core Program. Save the file and go to the terminal.
- Navigate to the directory in which Program.cs is located from the terminal. Type cd DotNetCoreSample and press Enter. Now, type the command dotnet run in the terminal and press Enter. You will see that the text My First .NET Core Program is being printed on the terminal.
Summary
We have now created a simple .NET Core console application using Visual Studio Code. We have leveraged the new .NET CLI commands to create and run the project. For more information about dotnet CLI refer this
link.
This article covers only the minimum setup for creating a development environment for .NET Core using Visual Studio Code. In future articles, I will talk about more cool VS Code extensions which will make .NET Core development using this awesome text editor more fun.