How To Code To Create An App
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
- Feedback
- Edit
Tutorial: Create a .NET console application using Visual Studio Code
- 7 minutes to read
Thank you.
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.
Prerequisites
- Visual Studio Code with the C# extension installed. For information about how to install extensions on Visual Studio Code, see VS Code Extension Marketplace.
- The .NET 6 SDK.
Create the app
Create a .NET console app project named "HelloWorld".
-
Start Visual Studio Code.
-
Select File > Open Folder (File > Open... on macOS) from the main menu.
-
In the Open Folder dialog, create a HelloWorld folder and select it. Then 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
. -
In the Do you trust the authors of the files in this folder? dialog, select Yes, I trust the authors.
-
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.
-
In the Terminal, enter the following command:
dotnet new console --framework net6.0
The project template creates a simple application that displays "Hello World" in the console window by calling the Console.WriteLine(String) method in Program.cs.
Console.WriteLine("Hello, World!");
-
Replace the contents of Program.cs with the following code:
namespace HelloWorld { class Program { static void Main(string[] args) { Console.WriteLine("Hello World!"); } } }
Select Yes if Visual Studio Code prompts you to add the missing assets to build and debug your app.
The 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.In the latest version of C#, a new feature named top-level statements lets you omit the
Program
class and theMain
method. Most existing C# programs don't use top-level statements, so this tutorial doesn't use this new feature. But it's available in C# 10, and whether you use it in your programs is a matter of style preference.
Run the app
Run the following command in the Terminal:
dotnet run
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.
-
Open Program.cs.
-
Replace the contents of the
Main
method in Program.cs, 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);
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 namedcurrentDate
. 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# 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 as interpolated strings. -
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.
-
Run the program again:
dotnet run
-
Respond to the prompt by entering a name and pressing the Enter key.
-
Press any key to exit the program.
Additional resources
- Setting up Visual Studio Code
Next steps
In this tutorial, you created a .NET console application. In the next tutorial, you debug the app.
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.
Prerequisites
- Visual Studio Code with the C# extension installed. For information about how to install extensions on Visual Studio Code, see VS Code Extension Marketplace.
- The .NET 5 SDK. If you install the .NET 6 SDK, install the .NET 5 SDK also, or some of the tutorial instructions won't work. For more information, see New C# templates generate top-level statements.
Create the app
Create a .NET console app project named "HelloWorld".
-
Start Visual Studio Code.
-
Select File > Open Folder (File > Open... on macOS) from the main menu.
-
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
. -
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.
-
In the Terminal, enter the following command:
dotnet new console --framework net5.0
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 code defines a class, Program
, with a single method, Main
, that takes a String array as an argument:
using System; namespace HelloWorld { class Program { static void Main(string[] args) { Console.WriteLine("Hello World!"); } } }
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
Run the following command in the Terminal:
dotnet run
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.
-
Open Program.cs by clicking on it.
The first time you open a C# file in Visual Studio Code, OmniSharp loads in the editor.
-
Select Yes when Visual Studio Code prompts you to add the missing assets to build and debug your app.
-
Replace the contents of the
Main
method in Program.cs, 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);
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 namedcurrentDate
. 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# 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 as interpolated strings. -
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.
-
Run the program again:
dotnet run
-
Respond to the prompt by entering a name and pressing the Enter key.
-
Press any key to exit the program.
Additional resources
- Setting up Visual Studio Code
Next steps
In this tutorial, you created a .NET console application. In the next tutorial, you debug the app.
This tutorial is only available for .NET 5 and .NET 6. Select one of those options at the top of the page.
Feedback
How To Code To Create An App
Source: https://docs.microsoft.com/en-us/dotnet/core/tutorials/with-visual-studio-code
Posted by: drakeimensid.blogspot.com
0 Response to "How To Code To Create An App"
Post a Comment