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.

requirements

  1. Visual Studio Code with the C # extension installed. For information about installing extensions for Visual Studio Code, see Marketplace for VS Code Extensions.
  2. The .NET 5.0 SDK or higher

Build the app

Create a .NET console app project named HelloWorld.

  1. Start Visual Studio Code.

  2. Select in the main menu file > Open folder (file > To open... under macOS).

  3. 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.

  4. 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.

  5. 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.

  1. Open Program.csby clicking on it.

    The first time you open a C # file in Visual Studio Code, OmniSharp loads in the editor.

  2. Choose Yes when Visual Studio Code prompts you to add the missing resources to build and debug your app.

  3. 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.

  4. Save the changes.

    Important

    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.

  5. Run the program again:

  6. Respond to the prompt by entering a name and pressing.

  7. Press any key to exit the program.

Additional resources

Next Steps

In this tutorial, you created a .NET console application. In the next tutorial, you'll debug the app.