Want to start C# programming but don't want to install the bulky Visual Studio? The lightweight and powerful editor VS Code can also perfectly handle C# development (actually, I'm just used to using VS Code).
Why choose VS Code for C# development?#
- Lightweight: The VS Code installation package is small, starts quickly, and uses few resources.
- Cross-platform: Supports various operating systems such as Windows, macOS, and Linux.
- Powerful extensibility: A rich set of extensions can meet various development needs, including C#.
Preparation#
Before you start, you need to ensure that the following software is installed on your computer:
- VS Code: Go to the VS Code official website to download and install the version suitable for your operating system.
- .NET SDK: Go to the .NET official website to download and install the .NET SDK. Please make sure to select SDK instead of Runtime.
Install C# extension in VS Code#
- Open VS Code.
- Click the "Extensions" icon on the left sidebar (it looks like four squares, so no image needed).
- Type "C#" in the search box.
- Find the extensions “C#”, “C# Dev Kit”, and “IntelliCode for C# Dev Kit”, and click the "Install" button.
- After installation, click the "Reload" button to activate the extension. (If necessary)
Create your first C# project#
-
Choose a suitable folder on your computer to store the C# project.
-
In VS Code, click “File” -> “Open Folder” and select the folder you just created.
-
Open the terminal in VS Code (you can open it via “Terminal” -> “New Terminal”).
-
In the terminal, enter the following command to create a new C# console application:
dotnet new console -o Learncs
This command will create a folder named
Learncs
and generate a basic C# project within it.
- In VS Code, click “File” -> “Open Folder” and select the newly created
Learncs
folder.
View project structure#
After opening the project, you can see the following files in the explorer view:
-
Program.cs: The main entry file for the C# application.
-
MyCSharpApp.csproj: The file that contains project configuration settings.
Run the first line of C# code provided by Microsoft#
-
In VS Code, open the
Program.cs
file. -
You will see the following default code:
// See https://aka.ms/new-console-template for more information Console.WriteLine("Hello, World!");
- If this is your first time using VS Code, you can press
Ctrl + F5
to run the code. - If there are conflicts with the shortcut keys, you can use the command
dotnet run
in the terminal to run all the code ordotnet run Program.cs
. - If everything goes smoothly, the terminal will print "Hello, World!"
Congratulations#
Now, we have successfully configured the C# development environment in VS Code and run the first line of C# code. By continuously learning and practicing, you will definitely be able to develop more complex and interesting C# applications.