Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Visual Studio Code

Introduction

Visual Studio Code (VS Code) is a lightweight and powerful code editor developed by Microsoft. It supports various programming languages and frameworks, including .NET. In this tutorial, we will cover how to set up and use Visual Studio Code for .NET development.

Installing Visual Studio Code

Download and install Visual Studio Code from the official website. Follow the installation instructions for your operating system.

Installing .NET SDK

Ensure you have the .NET SDK installed. You can download it from the .NET download page. Verify the installation by running:

dotnet --version

This command should output the installed .NET version.

Setting Up Visual Studio Code for .NET Development

Open Visual Studio Code and install the C# extension:

  1. Go to the Extensions view by clicking the square icon in the sidebar or pressing Ctrl+Shift+X.
  2. Search for "C#" and install the extension developed by Microsoft.

This extension provides .NET-specific features such as IntelliSense, debugging, and project management.

Creating a .NET Project

Create a new .NET project by opening a terminal in Visual Studio Code (Ctrl+`) and running:

dotnet new console -n MyFirstApp

This command creates a new console application named "MyFirstApp".

Open the project in Visual Studio Code:

code MyFirstApp

This command opens the MyFirstApp project in Visual Studio Code.

Running the Project

To run your project, open the terminal in Visual Studio Code and execute:

dotnet run

This command runs the application, and you should see "Hello, World!" printed to the terminal.

Debugging the Project

To debug your project, follow these steps:

  1. Open the Program.cs file and set a breakpoint by clicking in the left margin next to the line number.
  2. Go to the Run view by clicking the play icon in the sidebar or pressing Ctrl+Shift+D.
  3. Click "Run and Debug" and select ".NET Core" from the dropdown menu.

The debugger will start, and your application will run until it hits the breakpoint, allowing you to inspect variables and step through the code.

Adding a NuGet Package

To add a NuGet package to your project, use the terminal and run:

dotnet add package Newtonsoft.Json

This command installs the Newtonsoft.Json package into your project.

Use the package in your code:

using Newtonsoft.Json;

class Program
{
    static void Main(string[] args)
    {
        var json = JsonConvert.SerializeObject(new { name = "John", age = 30 });
        Console.WriteLine(json);
    }
}

This example uses the Newtonsoft.Json package to serialize an object to JSON and print it to the console.

Using Git for Version Control

Visual Studio Code integrates seamlessly with Git. To initialize a Git repository for your project:

git init

This command initializes a new Git repository in your project directory.

Use the Source Control view in Visual Studio Code to stage, commit, and push changes to your remote repository.

Conclusion

Visual Studio Code is a versatile and powerful code editor for .NET development. By following this tutorial, you should be able to set up a .NET project, run and debug your code, add packages, and use version control within Visual Studio Code. Happy coding!