Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit editor mode

Tutorial: Create a .NET console application using Visual Studio

Feedback

In this article

This tutorial shows how to create and run a .NET console application in Visual Studio 2022.

Prerequisites

Create the app

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

  1. Start Visual Studio 2022.

  2. On the start page, chooseCreate a new project.

    Create a new project button selected on the Visual Studio start page

  3. On theCreate a new project page, enterconsole in the search box. Next, chooseC# orVisual Basic from the language list, and then chooseAll platforms from the platform list. Choose theConsole App template, and then chooseNext.

    Create a new project window with filters selected

    Tip

    If you don't see the .NET templates, you're probably missing the required workload. Under theNot finding what you're looking for? message, choose theInstall more tools and features link. The Visual Studio Installer opens. Make sure you have the.NET desktop development workload installed.

  4. In theConfigure your new project dialog, enterHelloWorld in theProject name box. Then chooseNext.

    Configure your new project window with Project name, location, and solution name fields

  5. In theAdditional information dialog:

    • Select.NET 8.
    • SelectDo not use top-level statements.
    • SelectCreate.

    The template creates a simple application that displays "Hello, World!" in the console window. The code is in theProgram.cs orProgram.vb file:

    namespace HelloWorld; internal class Program {     static void Main(string[] args)     {         Console.WriteLine("Hello, World!");     } }
    Imports SystemModule Program    Sub Main(args As String())        Console.WriteLine("Hello World!")    End SubEnd Module

    If the language you want to use is not shown, change the language selector at the top of the page.

    The code defines a class,Program, with a single method,Main, that takes aString 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 theargs array.

    C# has a feature namedtop-level statements that lets you omit theProgram class and theMain method. This tutorial doesn't use this feature. Whether you use it in your programs is a matter of style preference.

Run the app

  1. PressCtrl+F5 to run the program without debugging.

    A console window opens with the text "Hello, World!" printed on the screen. (Or "Hello World!" without a comma in the Visual Basic project template.)

    Console window showing Hello World Press any key to continue

  2. Press any key to close the console window.

Enhance the app

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

  1. InProgram.cs orProgram.vb, replace the contents of theMain method, which is the line that callsConsole.WriteLine, with the following code:

    Console.WriteLine("What is your name?");var name = Console.ReadLine();var currentDate = DateTime.Now;Console.WriteLine($"{Environment.NewLine}Hello, {name}, on {currentDate:d} at {currentDate:t}!");Console.Write($"{Environment.NewLine}Press any key to exit...");Console.ReadKey(true);
    Console.WriteLine("What is your name?")Dim name = Console.ReadLine()Dim currentDate = DateTime.NowConsole.WriteLine($"{Environment.NewLine}Hello, {name}, on {currentDate:d} at {currentDate:t}")Console.Write($"{Environment.NewLine}Press any key to exit...")Console.ReadKey(True)

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

    Environment.NewLine is a platform-independent and language-independent way to represent a line break. Alternatives are\n in C# andvbCrLf 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 asinterpolated strings.

  2. PressCtrl+F5 to run the program without debugging.

  3. Respond to the prompt by entering a name and pressing theEnter key.

    Console window with modified program output

  4. Press any key to close the console window.

Additional resources

Next steps

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

Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, seeour contributor guide.

Feedback

Was this page helpful?

YesNoNo

Need help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?

  • Last updated on

In this article

Was this page helpful?

YesNo
NoNeed help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?