The idea of this article is to detail how to create a .NET Core Console application from the command line tool (.NET CLI – DOTNET Command Line Interface) and the Visual Studio Code.
The first step is to download the .NET Core SDK. Observe that it is possible to download it to multiple platforms, as for example:
- Windows
- Linux (RHEL, Ubuntu, Linux Mint, Debian, Fedora, CentOS, Oracle Linux and openSUSE)
- Mac
- Docker
The .NET Core SDK provides a set of libraries and tools to create applications and .NET Core libraries. After installation, by default, the .NET SDK is located in C:Program FilesdotnetSDK. Observe that in that resource the versions of .NET Core are installed side-by-side:
The .NET Core is based on a set of NuGet packages that allows you to optimize the applications to be dependent on only the necessary modules. This reduces the time required to test and deploy updates that are not used by the application.
Because the .NET Framework features are grouped under NuGet packages, the update time and delivery of these packages is much lower. It is not necessary to wait for a framework upgrade as a whole.
The NuGet packages installed by .NET Core SDK, are available for use in the .nugetpackages directory under the user profile:
To create a new application using the command line, open the command prompt (or PowerShell) and create a directory:
mkdir C: temp HelloWorldConsole
cd. Temp HelloWorldConsole
The .NET Core SDK provides a command-line tool (.NET Core CLI) through the executable (also called driver) dotnet. To list the commands available for the tool, type:
dotnet -h
Note that there are, by default, the following commands available:
To create a new application type Console you must use the new command. To list the types of projects that the command offers, enter the following command:
dotnet new -h
Notice that the new command provides the option to choose the language (C # or F #) through the -l parameter, and the type of project, through the -t parameter as:
- Console
- web
- lib
- xunittest
dotnet new -t console
- Program.cs
- project.json
namespace ConsoleApplication
{
public class Program
{
public static void Main (string [] args)
{
Console.WriteLine ( “Hello World!”);
}
}
}
{
“Version”, “1.0.0- *”
“BuildOptions”: {
“DEBUGTYPE”: “portable”, // reports will be generated if debugging information
“EmitEntryPoint”: true // tells whether the module contains an entry for the MAIN method
},
“Dependencies”: {}, // informs the project dependencies
“Frameworks”: {
“Netcoreapp1.0”: {// tells you what is the application framework
“Dependencies”: {
“Microsoft.NETCore.App”: {// use the super set that contains the .NET Standard Library and standard library
“Type”: “platform”, // tells the machine framework will be used
“Version”, “1.0.1”
}
},
“Imports”: “dnxcore50”
}
}
}
dotnet restore
{
“Locked”: false,
“Version”: 2
“Targets”: {
“.NETCoreApp, Version = v1.0”: {
“Libuv / 1.9.0”: {
“Type”: “package”
“Dependencies”: {
“Microsoft.NETCore.Platforms”: “1.0.1”
},
“RuntimeTargets”: {
“Runtimes / osx / native /_._”: {
“AssetType”: “native”
“Rid”: “osx”
}
}
},
“Microsoft.CodeAnalysis.Analyzers / 1.1.0”: {
“Type”: “package”
},
“Microsoft.CodeAnalysis.Common / 1.3.0”: {
“Type”: “package”
“Dependencies”: {
“Microsoft.CodeAnalysis.Analyzers” “1.1.0”
“System.AppContext” “4.1.0”
“System.Collections”: “4.0.11”
“System.Collections.Concurrent”: “4.0.12”
“System.Collections.Immutable” “1.2.0”
// … And several other references omitted here because of the amount
dotnet build
dotnet build -c release
{
“Version”, “1.0.0- *”
“BuildOptions”: {
“DEBUGTYPE”: “portable”, // reports will be generated if debugging information
“EmitEntryPoint”: true // tells whether the module contains an entry for the MAIN method
},
“Dependencies”: {}, // informs the project dependencies
“Frameworks”: {
“Net461”: {},
“Netcoreapp1.0”: {// tells you what is the application framework
“Dependencies”: {
“Microsoft.NETCore.App”: {// use the superset that contains the .NET Standard Library and standard library
“Type”: “platform”, // tells the machine framework will be used
“Version”, “1.0.1”
}
},
“Imports”: “dnxcore50”
}
}
}
- net461
- netcoreapp1.0
dotnet run
dotnet publish -c release
- C:TempHelloWorldConsolebinreleasenetcoreapp1.0publish
- C:TempHelloWorldConsolebinreleasenet461win7-x64 publish
HelloWorldConsole.exe
HelloWorldConsole.dll
dotnet HelloWorldConsole.dll
“Netcoreapp1.0”: {// tells you what is the application framework
“Dependencies”: {
“Microsoft.NETCore.App”: {// use the superset that contains the .NET Standard Library and standard library
“Type”: “platform”, // tells the machine framework will be used
“Version”, “1.0.1”
}
},
“Netcoreapp1.0”: {// tells you what is the application framework
“Dependencies”: {
“Microsoft.NETCore.App”: {// use the superset that contains the .NET Standard Library and standard library
“Version”, “1.0.1”
}
},
{
“Version”, “1.0.0- *”
“BuildOptions”: {
“DEBUGTYPE”: “portable”, // reports will be generated if debugging information
“EmitEntryPoint”: true // tells whether the module contains an entry for the MAIN method
},
“Dependencies”: {}, // informs the project dependencies
“Frameworks”: {
“Net461”: {},
“Netcoreapp1.0”: {// tells you what is the application framework
“Dependencies”: {
“Microsoft.NETCore.App”: {// use the superset that contains the .NET Standard Library and standard library
“Version”, “1.0.1”
}
},
“Imports”: “dnxcore50”
}
},
“Runtimes”: {
“Win7-x64”: {},
“Ubuntu.14.04-x64”: {}
}
}
dotnet restore
dotnet publish -c release
I hope you enjoyed.
References:
- Getting started with .NET Core on Windows / Linux / MacOS using the command line
- Writing .NET Core console apps using the CLI tools: An advanced step-by-step guide