|
| 1 | +--- |
| 2 | +title: Building a C# Hello World application with .NET Core in Visual Studio 2017 |
| 3 | +description: Learn how to build a simple .NET Core console application using Visual Studio 2017. |
| 4 | +keywords: .NET Core, .NET Core console application, Visual Studio 2017 |
| 5 | +author: BillWagner |
| 6 | +ms.author: wiwagn |
| 7 | +ms.date: 05/15/2017 |
| 8 | +ms.topic: article |
| 9 | +ms.prod: .net-core |
| 10 | +ms.technology: devlang-csharp |
| 11 | +ms.devlang: csharp |
| 12 | +ms.assetid: 97aa50bf-bdf8-416d-a56c-ac77504c14ea |
| 13 | +--- |
| 14 | + |
| 15 | +# Building a C# Hello World application with .NET Core in Visual Studio 2017 |
| 16 | + |
| 17 | +This topic provides a step-by-step introduction to building, debugging, and publishing a simple .NET Core console application using Visual Studio 2017. Visual Studio 2017 provides a full-featured development environment for building .NET Core applications. As long as the application doesn't have platform-specific dependencies, the application can run on any platform that .NET Core targets and on any system that has .NET Core installed. |
| 18 | + |
| 19 | +## Prerequisites |
| 20 | + |
| 21 | +[Visual Studio 2017](https://www.visualstudio.com/downloads/) with the ".NET Core cross-platform development" workload installed. |
| 22 | + |
| 23 | +For more information, see the [Prerequisites for .NET Core on Windows](../../core/windows-prerequisites.md) topic. |
| 24 | + |
| 25 | +## A simple Hello World application |
| 26 | + |
| 27 | +Begin by creating a simple "Hello World" console application. Follow these steps: |
| 28 | + |
| 29 | +1. Launch Visual Studio 2017. Select **File** > **New** > **Project** from the menu bar. In the **Add New Project** dialog, select the **.NET Core** node followed by the **Console App (.NET Core)** project template. In the **Name** text box, type "HelloWorld". Select the **OK** button. |
| 30 | + |
| 31 | +  |
| 32 | + |
| 33 | +1. Visual Studio loads the development environment. The C# Console Application template for .NET Core automatically defines a class, `Program`, with a single method, `Main`, that takes a <xref:System.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. |
| 34 | + |
| 35 | +  |
| 36 | + |
| 37 | + The template creates a simple "Hello World" application. It calls the <xref:System.Console.WriteLine(System.String)?displayProperty=fullName> method to display the literal string "Hello World!" in the console window. By selecting the **HelloWorld** button with the green arrow on the toolbar, you can run the program in Debug mode. If you do, the console window is visible for only a brief time interval before it closes. This occurs because the `Main` method terminates and the application ends as soon as the single statement in the `Main` method executes. |
| 38 | + |
| 39 | +1. To cause the application to pause before it closes the console window, add the following code immediately after the call to the <xref:System.Console.WriteLine(System.String)?displayProperty=fullName> method: |
| 40 | + |
| 41 | + ```csharp |
| 42 | + Console.Write("Press any key to continue..."); |
| 43 | + Console.ReadKey(true); |
| 44 | + ``` |
| 45 | + This code prompts the user to press any key and then pauses the program until a key is pressed. |
| 46 | + |
| 47 | +1. On the menu bar, select **Build** > **Build Solution**. This compiles your program into an intermediate language (IL) that's converted into binary code by a just-in-time (JIT) compiler. |
| 48 | + |
| 49 | +1. Run the program by selecting the **HelloWorld** button with the green arrow on the toolbar. |
| 50 | + |
| 51 | +  |
| 52 | + |
| 53 | +1. Press any key to close the console window. |
| 54 | + |
| 55 | +## Enhancing the Hello World application |
| 56 | + |
| 57 | +Enhance your application to prompt the user for their name and display it along with the date and time. To modify and test the program, do the following: |
| 58 | + |
| 59 | +1. Enter the following C# code in the code window immediately after the opening bracket that follows the `public static void Main(string[] args)` line and before the first closing bracket: |
| 60 | + |
| 61 | + [!code-csharp[GettingStarted#1](../../../samples/snippets/csharp/getting_started/with_visual_studio/helloworld.cs#1)] |
| 62 | + |
| 63 | +  |
| 64 | + |
| 65 | + This code displays "What is your name?" in the console window and waits until the user enters a string followed by the Enter key. It stores this string into a variable named `name`. It also retrieves the value of the <xref:System.DateTime.Now?displayProperty=fullName> property, which contains the current local time, and assigns it to a variable named `date`. Finally, it uses a [composite format string](../../standard/base-types/composite-format.md) to display these values in the console window. |
| 66 | + |
| 67 | +1. Compile the program by choosing **Build** > **Build Solution**. |
| 68 | + |
| 69 | +1. Run the program in Debug mode in Visual Studio by selecting the green arrow on the toolbar, pressing F5, or choosing the **Debug** > **Start Debugging** menu item. Respond to the prompt by entering a name and pressing the Enter key. |
| 70 | + |
| 71 | +  |
| 72 | + |
| 73 | +1. Press any key to close the console window. |
| 74 | + |
| 75 | +You've created and run your application. To develop a professional application, take some additional steps to make your application ready for release: |
| 76 | + |
| 77 | +- For information on debugging your application, see [Debugging your C# Hello World application with Visual Studio 2017](debugging-with-visual-studio.md). |
| 78 | + |
| 79 | +- For information on developing and publishing a distributable version of your application, see [Publishing your Hello World application with Visual Studio 2017](publishing-with-visual-studio.md). |
| 80 | + |
| 81 | +## Related topics |
| 82 | + |
| 83 | +Instead of a console application, you can also build a class library with .NET Core and Visual Studio 2017. For a step-by-step introduction, see [Building a class library with C# and .NET Core in Visual Studio 2017](library-with-visual-studio.md). |
| 84 | + |
| 85 | +You can also develop a .NET Core console app on Mac, Linux, and Windows by using [Visual Studio Code](https://code.visualstudio.com/), a downloadable code editor. For a step-by-step tutorial, see [Getting Started with Visual Studio Code](with-visual-studio-code.md). |
0 commit comments