1. 程式人生 > >在vs2017中用.net核建立一個c#Hello World的申請

在vs2017中用.net核建立一個c#Hello World的申請

This topic provides a step-by-step introduction to building, debugging,and publishing a simple.NET Coreconsole application using Visual Studio 2017. Visual Studio 2017 provides a full-featured development environment for building .NET Core applications. As long as theapplication doesn’t have platform-specific dependencies, the application can run on any platform that .NET Coretargets and on any system that has .NET Coreinstalled.
【Prerequisites】
   Visual Studio 2017 with the”.NET Corecross-platform development” workload installed. For more information, see the Prerequisites for .NET Core on Windows topic.
【A simple Hello World application】
   Begin by creating a simple”Hello World” console application.Follow these steps:
(1) Launch Visual Studio 2017.SelectFile > 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.
這裡寫圖片描述

(2) 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 String array as an argument. Main is the application entry point, the method that’s called automatically by the runtime when it launches theapplication. Any command-linearguments supplied when the application is launched are availablein the args array.
這裡寫圖片描述

The template creates a simple”Hello World”application. It calls the System.Console.WriteLine(String) 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.
(3)To cause the application to pause before it closes the console window,add the following code immediately after the call to the System.Console.WriteLine(String) method:
這裡寫圖片描述

This code prompts the user to press any key and then pauses the program until a key is pressed.
(4)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.
(5)Run the program by selecting the HelloWorld button with the green arrow on the toolbar.
這裡寫圖片描述
(6) Press any key to closetheconsole window.
【Enhancing the Hello World application】
   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:
(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:
這裡寫圖片描述
這裡寫圖片描述
   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 System.DateTime.Now property, which contains the current local time,and assigns it to a variable named date .Finally, it uses a composite format string to display the sevalues in the console window.
(2)Compile the program by choosing Build > Build Solution.
(3)Run the program in Debug modein 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 nameand pressing theEnter key.
這裡寫圖片描述
(4)Press any key to closetheconsole window.
You’ve created and run your application.To develop a professional application, take some additional steps to make your application ready for release:
● For information on debugging your application, see Debugging your C# Hello World application with Visual Studio 2017.
● For information on developing and publishing a distributable version of your application, see Publishing your Hello World application with Visual Studio 2017.
【Related topics 】
   Instead of a console application,you can also build a class library with .NET Coreand Visual Studio 2017.For a step-by-step introduction, see Building a class library with C# and .NET Core in Visual Studio 2017.
   You can also develop a .NET Coreconsole app on Mac,Linux,and Windows by using Visual Studio Code,a downloadable code editor.For a step-by-step tutorial, see Getting Started with Visual Studio Code.