KB Controls is reader-supported. When you buy through links on our site, we may earn an affiliate commission. Read more in our disclaimer

How to Work with C# in Visual Studio Code

C# From Scratch Part 2.0. In this part of the series, we learn how to set up Visual Studio Code, edit our C# code, run our application through VS Code.

Welcome to another edition of C# From Scratch, a course dedicated to teaching you everything you need to know to be productive with C#. If you’re new here, head over to the index to see all parts of the series.

In the last part of the series, we learned how to run a C# .NET Console application.

In this part of the series, we’ll get set up with our code editor, Visual Studio Code, and learn how to edit C# code.

Install Visual Studio Code

In a previous part of the series, we learned about the .NET SDK and installed it on our machines. Just like we use the .NET SDK to easily create .NET applications, we will install a code editor to easily edit the code for our .NET applications.

The code editor that we will use in this series is Visual Studio Code. Visual Studio Code is a free, cross platform code editor developed by Microsoft. You can download Visual Studio Code here.

Run the installer to install Visual Studio Code. For convenience, I recommend adding Visual Studio Code to your PATH while installing it.

Add Visual Studio Code to your PATH

With Visual Studio Code installed, we can start to edit our C# application.

Configure Visual Studio Code

To launch Visual Studio Code, switch into the Hello directory where we created our application and launch Visual Studio Code by using the shortcut "code .". This command tells Visual Studio Code to start in the current directory (that's what the "." means).

If prompted to verify that you trust the author of the files in this folder, choose "Yes, I trust the authors".

Launch Visual Studio Code with "code ." and choose to trust the authors

If you didn't add Visual Studio Code to your PATH, then you can launch Visual Studio Code from the start menu and browse to the application using File > Open Folder.

C# support is an optional extension in Visual Studio Code. To install the C# extension, jump into the extensions tab, search for C#, and install the C# extension from Microsoft.

Install the C# extension for Visual Studio Code

Once the installation has finished, close and relaunch Visual Studio Code. After a short delay, you should see a prompt that informs you that some assets required to build and debug the project are missing. Click "Yes" to add the missing assets. These assets enable us to debug our project in Visual Studio Code more easily. We'll talk about debugging in Visual Studio Code soon.

Install missing assets

Editing C# Code in Visual Studio Code

Now that we have Visual Studio Code set up, let's have a look at the code that makes up our application.

In the Explorer pane on the left of Visual Studio Code, you see all the files and folders that make up your application. Program.cs is currently the only file that contains C# source code. Click on it to open the C# source code file in the main pane of Visual Studio Code.

Open Program.cs

Without knowing anything about C#, we can easily understand where the output from our application is coming from. On line 9, we see the string "Hello World" in parentheses after some keywords. A string in C# is any sequence of characters enclosed in double quotes.

Update the string to create a personalized greeting.

Create a personalized greeting

Running the Application with Visual Studio Code

To view the updated output of the application, you could go back to the Command Prompt window and run the application. You can also run the application in Visual Studio Code by clicking on Run > Run Without Debugging or use the keyboard shortcut Ctrl + F5.

Run without Debugging

The first time you run your application, you will be prompted to choose an environment. For cross-platform applications, you should always choose the ".NET 5+ and .NET Core" option.

After selecting the option, the launch.json file opens which contains configuration information about your application. You can close this file and run the application again. The application runs and you should see your personalized greeting in the Debug Console window at the bottom of Visual Studio Code.

Updated Output

Conclusion

In this part of the series, we installed and configured Visual Studio Code. We also saw how to edit our code and run out application in Visual Studio Code.

In the next part of the series, we will extend our application to make it interactive. Instead of printing a hardcoded greeting to the console, the application will accept input from a user and update the output based on this input.

Learn to Program Logix 5000 PLCs

Learn how to write and test your first PLC program using Studio 5000 Logix Designer.

Learn More
PLC Programming Bootcamp

Learn how to write, test, and simulate your first PLC program for free.

PLC Bootcamp

Related Content