Microsoft Visual Code



Code

-->

This tutorial shows how to create and run a .NET console application by using Visual Studio Code and the .NET CLI. Project tasks, such as creating, compiling, and running a project are done by using the .NET CLI. You can follow this tutorial with a different code editor and run commands in a terminal if you prefer.

Use Pretty listing (reformatting) of code to reformat your code in the code editor. For more information, see Options, Text Editor, Basic (Visual Basic). Use only one statement per line. Don't use the Visual Basic line separator character (:). Visual Studio IDE Visual Studio for Mac Visual Studio Code To continue downloading, click here Visual Studio Live Share Visual Studio 2020-12-30T07:30:11-08:00. Welcome to the March 2021 release of Visual Studio Code. There are a number of updates in this version that we hope you will like, some of the key highlights include: Accessibility improvements - Multi-cursor support and increased line limit. Updated icons for macOS Big Sur - Brand icons that match the visual style of Big Sur. If you’re not familiar, Visual Studio Code (VS Code) is an open source C development environment from Microsoft. It is available for Windows, macOS and x64 Linux, and now you can run it on.

Prerequisites

Microsoft Visual Code Vs Visual Studio

  1. Visual Studio Code with the C# extension installed. For information about how to install extensions on Visual Studio Code, see VS Code Extension Marketplace.
  2. The .NET 5.0 SDK or later
Microsoft Visual Code

Create the app

Microsoft Visual Code Python

Create a .NET console app project named 'HelloWorld'.

  1. Start Visual Studio Code.

  2. Select File > Open Folder (File > Open... on macOS) from the main menu.

  3. In the Open Folder dialog, create a HelloWorld folder and click Select Folder (Open on macOS).

    The folder name becomes the project name and the namespace name by default. You'll add code later in the tutorial that assumes the project namespace is HelloWorld.

  4. Open the Terminal in Visual Studio Code by selecting View > Terminal from the main menu.

    The Terminal opens with the command prompt in the HelloWorld folder.

  5. In the Terminal, enter the following command:

Microsoft Visual Code Tutorial

The template creates a simple 'Hello World' application. It calls the Console.WriteLine(String) method to display 'Hello World!' in the console window.

Visual

The template code defines a class, Program, with a single method, Main, that takes a String array as an argument:

Main is the application entry point, the method that's called automatically by the runtime when it launches the application. Any command-line arguments supplied when the application is launched are available in the args array.

Run the app

MicrosoftMicrosoft Visual Code

Run the following command in the Terminal:

The program displays 'Hello World!' and ends.

Enhance the app

Enhance the application to prompt the user for their name and display it along with the date and time.

  1. Open Program.cs by clicking on it.

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

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

  3. Replace the contents of the Main method in Program.cs, which is the line that calls Console.WriteLine, with the following code:

    This code displays a prompt in the console window and waits until the user enters a string followed by the Enter key. It stores this string in a variable named name. It also retrieves the value of the DateTime.Now property, which contains the current local time, and assigns it to a variable named date. And it displays these values in the console window. Finally, it displays a prompt in the console window and calls the Console.ReadKey(Boolean) method to wait for user input.

    NewLine is a platform-independent and language-independent way to represent a line break. Alternatives are n in C# and vbCrLf in Visual Basic.

    The dollar sign ($) in front of a string lets you put 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 referred to as interpolated strings.

  4. Save your changes.

    Important

    In Visual Studio Code, you have to explicitly save changes. Unlike Visual Studio, file changes are not automatically saved when you build and run an app.

  5. Run the program again:

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

  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 debug the app.