An Easy Hands-On Guide to Creating and Running Console Applications with .NET CLI

.NET Core, developed by Microsoft, serves as a powerful and open-source framework for building modern, cross-platform applications. If you’re eager to dive into the world of console application development, .NET Core and its Command-Line Interface (CLI) tools provide an accessible and efficient way to get started. In the article, we will explore how to create a console application using the .NET CLI

Setting the Stage: Downloading .NET Core

Begin your journey by downloading the latest version of .NET Core from the official website dotnet.microsoft.com. The installation process is straightforward and tailored to various operating systems, including Windows, macOS, and Linux.

console application using dotnet cli
Console Application using .NET CLI

Unveiling the CLI Magic

At the heart of .NET Core lies the Command-Line Interface (CLI) tools, offering a suite of commands to simplify the development process. These tools are indispensable for creating, building, and running applications from the command line, providing a streamlined alternative to graphical interfaces.

The .NET CLI operates on a set of commands, each serving a specific purpose. Here are a few fundamental commands:

  • dotnet new: Initializes a new project based on a specified template.
  • dotnet build: Compiles the source code of a project.
  • dotnet run: Builds and runs the project.
  • dotnet publish: Publishes the application for deployment.

Command Structure

CLI commands typically follow this structure:

dotnet [command] [arguments] [options]

For example, to create a new console application, you would use:

dotnet new console -n MyConsoleApp

This command initializes a new console application named “MyConsoleApp.”

Exploring Installed Project Templates

To list the installed project templates execute the following command

dotnet new --list

Here is the sample output on my machine, this may differ as per your installed version

 dotnet new --list
Warning: use of 'dotnet new --list' is deprecated. Use 'dotnet new list' instead.
For more information, run:
   dotnet new list -h

These templates matched your input:

Template Name                                 Short Name           Language    Tags
--------------------------------------------  -------------------  ----------  --------------------------------
ASP.NET Core Empty                            web                  [C#],F#     Web/Empty
ASP.NET Core gRPC Service                     grpc                 [C#]        Web/gRPC
ASP.NET Core Web API                          webapi               [C#],F#     Web/WebAPI
ASP.NET Core Web App                          webapp,razor         [C#]        Web/MVC/Razor Pages
ASP.NET Core Web App (Model-View-Controller)  mvc                  [C#],F#     Web/MVC
ASP.NET Core with Angular                     angular              [C#]        Web/MVC/SPA
ASP.NET Core with React.js                    react                [C#]        Web/MVC/SPA
Blazor Server App                             blazorserver         [C#]        Web/Blazor
Blazor Server App Empty                       blazorserver-empty   [C#]        Web/Blazor/Empty
Blazor WebAssembly App                        blazorwasm           [C#]        Web/Blazor/WebAssembly/PWA
Blazor WebAssembly App Empty                  blazorwasm-empty     [C#]        Web/Blazor/WebAssembly/PWA/Empty
Class Library                                 classlib             [C#],F#,VB  Common/Library
Console App                                   console              [C#],F#,VB  Common/Console
dotnet gitignore file                         gitignore                        Config
Dotnet local tool manifest file               tool-manifest                    Config
EditorConfig file                             editorconfig                     Config
global.json file                              globaljson                       Config
MSBuild Directory.Build.props file            buildprops                       MSBuild/props
MSBuild Directory.Build.targets file          buildtargets                     MSBuild/props
MSTest Test Project                           mstest               [C#],F#,VB  Test/MSTest
MVC ViewImports                               viewimports          [C#]        Web/ASP.NET
MVC ViewStart                                 viewstart            [C#]        Web/ASP.NET
NuGet Config                                  nugetconfig                      Config
NUnit 3 Test Item                             nunit-test           [C#],F#,VB  Test/NUnit
NUnit 3 Test Project                          nunit                [C#],F#,VB  Test/NUnit
Protocol Buffer File                          proto                            Web/gRPC
Razor Class Library                           razorclasslib        [C#]        Web/Razor/Library
Razor Component                               razorcomponent       [C#]        Web/ASP.NET
Razor Page                                    page                 [C#]        Web/ASP.NET
Solution File                                 sln,solution                     Solution
Web Config                                    webconfig                        Config
Windows Forms App                             winforms             [C#],VB     Common/WinForms
Windows Forms Class Library                   winformslib          [C#],VB     Common/WinForms
Windows Forms Control Library                 winformscontrollib   [C#],VB     Common/WinForms
Worker Service                                worker               [C#],F#     Common/Worker/Web
WPF Application                               wpf                  [C#],VB     Common/WPF
WPF Class Library                             wpflib               [C#],VB     Common/WPF
WPF Custom Control Library                    wpfcustomcontrollib  [C#],VB     Common/WPF
WPF User Control Library                      wpfusercontrollib    [C#],VB     Common/WPF
xUnit Test Project                            xunit                [C#],F#,VB  Test/xUnit

Customizing Project Creation

Let’s say you want to create a web application using the ASP.NET Core template. You would use:

dotnet new web -n MyWebApp

Creating and Running a Console Application

Now, let’s walk through creating and running a console application:

1. Initialize a New Console Application

dotnet new console -n "MyApp"

PS C:\test> dotnet new console -n "MyApp"
The template "Console App" was created successfully.

Processing post-creation actions...
Restoring C:\test\MyApp\MyApp.csproj:
  Determining projects to restore...
C:\test\MyApp\MyApp.csproj : warning NU1803: You are running the 'restore' operation with an 'HTTP' source, 'http://im1nuget/nuget'. Non-HTTPS access will be removed in a future version. Consider migrating to
 an 'HTTPS' source.
  Restored C:\test\MyApp\MyApp.csproj (in 127 ms).
Restore succeeded.

Replace “MyApp” with your chosen project name. The command automatically restores the project dependencies.

2. Navigate to the Project Directory

cd MyApp

3. Build and Run the Application

dotnet run

PS C:\test\MyApp> dotnet run
C:\test\MyApp\MyApp.csproj : warning NU1803: You are running the 'restore' operation with an 'HTTP' source, 'http://im1nuget/nuget'. Non-HTTPS access will be removed in a future version. Consider migrating to
 an 'HTTPS' source.
C:\test\MyApp\MyApp.csproj : warning NU1803: You are running the 'restore' operation with an 'HTTP' source, 'http://im1nuget/nuget'. Non-HTTPS access will be removed in a future version. Consider migrating to
 an 'HTTPS' source.
Hello, World!

On running the application we get the above output.

Conclusion

In essence, the .NET CLI is not just a set of commands; it’s a gateway to efficient, command-based development. Embrace the CLI journey, experiment with different commands, and unlock the full potential of .NET Core in your projects. The examples provided are just a glimpse into the vast capabilities of the CLI tools—there’s a world of possibilities waiting for you to explore.

I hope you find this post helpful. Cheers!!!

[Further Readings: FHIR ResearchSubject Resource |  FHIR ResearchStudy Resource |  FHIR TestReport Resource |  FHIR TestScript Resource | FHIR TestPlan Resource |  FHIR MeasureReport Resource |  FHIR Measure Resource |  FHIR EvidenceVariable Resource |  FHIR EvidenceReport Resource | FHIR Evidence Resource | FHIR Citation Resource | FHIR ArtifactAssessment Resource | FHIR VerificationResult Resource | FHIR InventoryReport Resource |  FHIR OrganizationAffiliation Resource | FHIR SupplyDelivery Resource |  FHIR SupplyRequest Resource |  FHIR GuidanceResponse Resource |  FHIR DeviceAssociation Resource | FHIR DeviceDispense Resource  | FHIR DeviceRequest Resource   | FHIR QuestionnaireResponse Resource |  FHIR Questionnaire Resource |  FHIR PlanDefinition Resource |  FHIR Task Resource | FHIR RegulatedAuthorization Resource |  FHIR ManufacturedItemDefinition Resource |  FHIR AdministrableProductDefinition Resource |  FHIR PackagedProductDefinition Resource |  FHIR ClinicalUseDefinition Resource | Dependency Injection in WPF ]

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x