| title | Getting started with .NET Core using the CLI | |
|---|---|---|
| description | A step-by-step tutorial showing how to get started with .NET Core on Windows, Linux, or macOS using the .NET Core command-line interface (CLI). | |
| keywords | .NET Core, CLI | |
| author | cartermp | |
| ms.author | mairaw | |
| ms.date | 03/08/2017 | |
| ms.topic | get-started-article | |
| ms.prod | .net-core | |
| ms.technology | dotnet-cli | |
| ms.devlang | dotnet | |
| ms.assetid | 41632e63-d5c6-4427-a09e-51dc1116d45f | |
| ms.workload |
|
This topic will show you how to start developing cross-platforms apps in your machine using the .NET Core CLI tools.
If you're unfamiliar with the .NET Core CLI toolset, read the .NET Core SDK overview.
- .NET Core SDK 1.0.
- A text editor or code editor of your choice.
You can view or download the sample code from the dotnet/docs GitHub repository. For download instructions, see Samples and Tutorials.
Open a command prompt and create a folder named Hello. Navigate to the folder you created and type the following:
$ dotnet new console
$ dotnet restore
$ dotnet run
Let's do a quick walkthrough:
-
$ dotnet new consoledotnet newcreates an up-to-dateHello.csprojproject file with the dependencies necessary to build a console app. It also creates aProgram.cs, a basic file containing the entry point for the application.Hello.csproj:[!codeHello.csproj]
The project file specifies everything that's needed to restore dependencies and build the program.
- The
OutputTypetag specifies that we're building an executable, in other words a console application. - The
TargetFrameworktag specifies what .NET implementation we're targeting. In an advance scenario, you can specify multiple target frameworks and build to all those in a single operation. In this tutorial, we'll stick to building only for .NET Core 1.0.
Program.cs:[!code-csharpProgram.cs]
The program starts by
using System, which means "bring everything in theSystemnamespace into scope for this file". TheSystemnamespace includes basic constructs such asstring, or numeric types.We then define a namespace called
Hello. You can change this to anything you want. A class namedProgramis defined within that namespace, with aMainmethod that takes an array of strings as its argument. This array contains the list of arguments passed in when the compiled program is called. As it is, this array is not used: all the program is doing is to write "Hello World!" to the console. Later, we'll make changes to the code that will make use of this argument.[!INCLUDEDotNet Restore Note]
- The
-
$ dotnet restoredotnet restorecalls into NuGet (.NET package manager) to restore the tree of dependencies. NuGet analyzes the Hello.csproj file, downloads the dependencies stated in the file (or grabs them from a cache on your machine), and writes the obj/project.assets.json file. The project.assets.json file is necessary to be able to compile and run.The project.assets.json file is a persisted and complete set of the graph of NuGet dependencies and other information describing an app. This file is read by other tools, such as
dotnet buildanddotnet run, enabling them to process the source code with a correct set of NuGet dependencies and binding resolutions. -
$ dotnet rundotnet runcallsdotnet buildto ensure that the build targets have been built, and then callsdotnet <assembly.dll>to run the target application.$ dotnet run Hello World!Alternatively, you can also execute
dotnet buildto compile the code without running the build console applications. This results in a compiled application as a DLL file that can be run withdotnet bin\Debug\netcoreapp1.0\Hello.dllon Windows (use/for non-Windows systems). You may also specify arguments to the application as you'll see later on the topic.$ dotnet bin\Debug\netcoreapp1.0\Hello.dll Hello World!As an advanced scenario, it's possible to build the application as a self-contained set of platform-specific files that can be deployed and run to a machine that doesn't necessarily have .NET Core installed. See .NET Core Application Deployment for details.
Let's change the program a bit. Fibonacci numbers are fun, so let's add that in addition to use the argument to greet the person running the app.
-
Replace the contents of your Program.cs file with the following code:
[!code-csharpFibonacci]
-
Execute
dotnet buildto compile the changes. -
Run the program passing a parameter to the app:
$ dotnet run -- John Hello John! Fibonacci Numbers 1-15: 1: 0 2: 1 3: 1 4: 2 5: 3 6: 5 7: 8 8: 13 9: 21 10: 34 11: 55 12: 89 13: 144 14: 233 15: 377
And that's it! You can augment Program.cs any way you like.
Single files are fine for simple one-off programs, but if you're building a more complex app, you're probably going to have multiple source files on your project Let's build off of the previous Fibonacci example by caching some Fibonacci values and add some recursive features.
-
Add a new file inside the Hello directory named FibonacciGenerator.cs with the following code:
[!code-csharpFibonacci Generator]
-
Change the
Mainmethod in your Program.cs file to instantiate the new class and call its method as in the following example:[!code-csharpNew Program.cs]
-
Execute
dotnet buildto compile the changes. -
Run your app by executing
dotnet run. The following shows the program output:0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
And that's it! Now, you can start using the basic concepts learned here to create your own programs.
Note that the commands and steps shown in this tutorial to run your application are used during development time only. Once you're ready to deploy your app, you'll want to take a look at the different deployment strategies for .NET Core apps and the dotnet publish command.
Organizing and testing projects with the .NET Core CLI tools