Building console apps using Visual Studio Code
The goal of this section is to showcase how to build a console app using Visual Studio Code. Both instructions and screenshots in this section are for macOS, but the same actions will work with Visual Studio Code on Windows and Linux variants.
The main differences will be native command-line actions such as deleting a file: both the command and the path are likely to be different on Windows or macOS and Linux. Luckily, the dotnet
command-line tool will be identical on all platforms.
Writing code using Visual Studio Code
Let's get started writing code!
- Start Visual Studio Code.
- On macOS, navigate to File | Open..., or press Cmd + O. On Windows, navigate to File | Open Folder…, or press Ctrl + K Ctrl + O. On both OSes, you can click the Open Folder button in the EXPLORER pane or click the Open Folder… link on the Welcome page, as shown in the following screenshot:
- In the dialog box, navigate to your user folder on macOS (mine is named
markjprice
), yourDocuments
folder on Windows, or any directory or drive in which you want to save your projects. - Click the New Folder button and name the folder
Code
. - In the
Code
folder, create a new folder namedChapter01
. - In the
Chapter01
folder, create a new folder namedHelloCS
. - Select the
HelloCS
folder and on macOS click Open or on Windows click Select Folder. - Navigate to View | Terminal, or on macOS press Ctrl + ` (backtick) and on Windows press Ctrl + ' (single quote). Confusingly on Windows the key combination Ctrl + ` (backtick) splits the current window!
- In TERMINAL, enter the following command:
dotnet new console
- You will see that the
dotnet
command-line tool creates a new Console Application project for you in the current folder, and the EXPLORER window shows the two files created,HelloCS.proj
andProgram.cs
, as shown in the following screenshot: - In EXPLORER, click on the file named
Program.cs
to open it in the editor window. The first time that you do this, Visual Studio Code may have to download and install C# dependencies like OmniSharp, the Razor Language Server, and the .NET Core debugger, if it did not do this when you installed the C# extension. - If you see a warning saying that required assets are missing, click Yes, as shown in the following screenshot:
- After a few seconds, a folder named
.vscode
will appear in the EXPLORER pane. These are used during debugging, as you will learn in Chapter 4, Writing, Debugging, and Testing Functions. - In
Program.cs
, modify line 9 so that the text that is being written to the console says, Hello, C#! - Navigate to File | Auto Save. This toggle will save the annoyance of remembering to save before rebuilding your application each time.
Compiling and running code using dotnet CLI
The next task is to compile and run the code.
- Navigate to View | Terminal and enter the following command:
dotnet run
- The output in the TERMINAL window will show the result of running your application, as shown in the following screenshot: