How to use Visual Studio Code
Tutorial: Creating a .NET Console Application Using Visual Studio Code
- 2 minutes to read
This tutorial shows how to build and run a .NET console application by using Visual Studio Code and the .NET CLI. Project tasks such as building, compiling, and running a project are done using the .NET CLI. If you wish, you can follow this tutorial with another code editor and execute commands in a terminal.
- Visual Studio Code with the C # extension installed. For information about installing extensions for Visual Studio Code, see Marketplace for VS Code Extensions.
- The .NET 5.0 SDK or higher
Build the app
Create a .NET console app project named HelloWorld.
Start Visual Studio Code.
Select in the main menu file > Open folder (file > To open... under macOS).
Create in the dialog box Open folder the folder HelloWorldand click Select folder (to open under macOS).
The name of the folder becomes the project name and namespace name by default. You'll add code later in this tutorial that expects the project namespace to be.
Open that terminal in Visual Studio Code by going to the main menu view > terminal choose.
The terminal appears with the command prompt in the folder HelloWorld open.
Enter im terminal the following command:
The template creates a simple "Hello World" application. It calls the Console.WriteLine (String) method to display "Hello World!" In the console window.
The template's code defines the class with the single method that takes an array of strings as an argument:
is the entry point of the application, the method that is automatically called by the runtime when it starts the application. All command line arguments provided when the application starts are in the args-Array available.
Run the app
Run the following commands in terminal out:
The program displays "Hello World!" and is terminated.
Optimizing the app
Expand the application to prompt the user for their name and display it along with the date and time.
Open Program.csby clicking on it.
The first time you open a C # file in Visual Studio Code, OmniSharp loads in the editor.
Choose Yes when Visual Studio Code prompts you to add the missing resources to build and debug your app.
Replace the contents of the method in Program.cs, which consists of the line that calls, by the following code:
This code displays a prompt in the console window and waits for the user to enter a string and press that. The code stores this string in a variable named. It also gets the value of the DateTime.Now property, which contains the current local time, and assigns the value to a variable named. These values are also displayed in the console window. Eventually, a prompt appears in the console window and the Console.ReadKey (Boolean) method is called to wait for user input.
NewLine is a platform and language-independent way of displaying a line break. Alternatives are in C # and in Visual Basic.
You can use the dollar sign () in front of a string to enclose expressions such as variable names in curly braces in the string. The expression value is inserted into the string in place of the expression. This syntax is known as interpolated strings.
Save the changes.
In Visual Studio Code, you must explicitly save changes. Unlike Visual Studio, file changes are not automatically saved when you create and run an app.
Run the program again:
Respond to the prompt by entering a name and pressing.
Press any key to exit the program.
In this tutorial, you created a .NET console application. In the next tutorial, you'll debug the app.
- How does GrubHub pay for restaurants
- What is MB
- What is the correct pronunciation of hexakosioihexekontahexaphobia
- How is the sea level measured
- Electronic cigarettes are being marketed to young people
- What's going on in Pakistani politics
- How is nakula special in Mahabharata
- Rick Perry is a good Secretary of Energy
- What about TDS
- What's the next scientific breakthrough
- Why are internet users still judging us?
- What does the pupil size indicate in the anime
- Does India need a new opposition?
- What's your smallest claim to fame
- What are the advantages of OS
- Is an investment in gold useful and profitable?
- How brutal is the Vietnamese mafia
- Why do I hear people talking
- What made the southerners develop a distinct accent
- Which sects in Christianity are actually valid
- Why does everyone hate the United States
- How important are the tenth board exams
- Was Thai ever written in Chinese characters?
- This is how ending polls works