1. Install the necessary extensions
- Open VSCode.
- Click the extension icon on the left (or press
Ctrl+Shift+X
)。 - Search and install
C#
Expand. This extension is provided by Microsoft and provides basic functions required for C# development, such as intelligent perception, code navigation, debugging, etc.
2. Create a project directory
- In the file system, create a new folder, which will be your project folder. For example, you can create a name called
MyCSharpProject
folder.
3. Open the project directory
- In VSCode, click
File
->Open Folder
, and select the project folder you just created.
4. Initialize C# project
- Open the integrated terminal of VSCode (click
View
->Terminal
Or press `Ctrl+``). - In the terminal, enter the following command to create a new C# project:
dotnet new console
This will usedotnet
Command line tools create a new console application. If you want to create a Core project, you can usedotnet new web
ordotnet new mvc
etc. Select the commands according to the specific project type.
5. Compile the project
- Continue in the integration terminal, compile the project with the following command:
dotnet build
This command compiles the C# code into an executable or library file, depending on your project type. The compiled results will be stored inbin
in the directory.
VI. Start the project
- For console applications, use the following command to run:
dotnet run
This will execute the compiled executable file and you will see the output of the program.
7. Debugging project (optional)
- Set a breakpoint in the code, click on the left area of the code line number to set the breakpoint.
- Click the debug icon on the left (or press
Ctrl+Shift+D
)。 - In the Debug Configuration drop-down menu at the top, select
.NET Core Launch (console)
。 - Click the green play button to start debugging, and the program will run to the breakpoint you set. At this time, you can use the debugging toolbar to perform single steps, view variable values, etc.
Here is a simple C# console application sample code:
using System; namespace MyCSharpProject { class Program { static void Main(string[] args) { ("Hello, World!"); } } }
Code explanation:
-
using System;
:IntroducedSystem
Namespace, which contains some basic types and functions, such asConsole
kind. -
namespace MyCSharpProject
: Defines a namespace for organizing code to avoid naming conflicts. -
class Program
: Define a name calledProgram
Class of . -
static void Main(string[] args)
: The entry point of the program is where the program starts to execute. -
("Hello, World!");
:useConsole
ClassicWriteLine
Method outputs text to the console.
Through the above steps, you can create, compile, and start a C# project in VSCode. As needed, you can modify the code, add more classes and files, and use more.NET
Functions and libraries.
Summarize
This is the end of this article about using VSCode to create and start a C# project. For more related content on creating and starting a C# project in VSCode, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!