Introduction on How to Use .NET SDK CLI for Software Development on .NET Platform Using C# and VB.NET
by xanthium-enterprises in Circuits > Microsoft
218 Views, 0 Favorites, 0 Comments
Introduction on How to Use .NET SDK CLI for Software Development on .NET Platform Using C# and VB.NET
Here we are going to learn develop software for the .NET (dotnet) Platform using command line tools provided by the .NET SDK CLI using the languages C# or Visual Basic.NET
Here all the software development will be done using Command line tools like dotnet and no IDE's like Visual Studio Community or Visual Studio Code will be used.
The tutorial is aimed at beginners who wants to get started with the .NET(dotnet) platform
The tutorial is cross platform and will work on both Windows and Linux systems ,provided .NET SDK is installed.
You can find the full article about .NET Platform software development using .NET SDK CLI tools here.
Contents of this Instructable
- Brief History Of The .NET Platform
- Installing .NET SDK On Your Computer
- .NET SDK CLI Tools Command Structure
- Creating A Simple Console Project Using .NET SDK
- Choosing A Programming Language To Develop Your .NET App
- Creating A WinForms Project Using .NET SDK
- Creating A WPF Project Using .NET SDK
- Creating A Web Application Using ASP.NET Core
- How To Add References To Required .Net Libraries
- Running Your .NET Project
- Create An Executable Using .NET SDK CLI
Supplies
- You will need a computer running Windows/Linux /Mac OS .
- A text editor like Notepad++ or Sublime Text. You can also use the default provided with the OS like note pad or VI
- You need to install the latest .NET SDK from Microsoft Website
Brief History of the .NET Platform
The .NET Framework (dotnet framework) is a proprietary software framework developed by Microsoft that runs primarily on Microsoft Windows. It provides a comprehensive programming model and runtime environment for building and running various types of (console, Libraries, Windows Form,WPF) applications on Windows-based systems.
Key components of the .NET Framework are
- Common Language Runtime (CLR) The CLR is the execution engine of the .NET Framework. It provides services such as memory management, exception handling, and security.
- Base Class Library (BCL) The BCL is a collection of reusable classes, types, and APIs that provide fundamental functionality for .NET applications. It includes classes for working with data, files, networking, and more.
The .NET Framework is included by default on all Windows Releases and will continue to be included with future releases of Windows and continue to receive security updates in the near future.
In April 2019, Microsoft released .NET Framework 4.8, the last major version of the framework as a proprietary offering, followed by .NET Framework 4.8.1 in August 2022.
It supports languages like C#,VB.NET and F# that compile down to a platform independent bytecode that runs on the Common Language Runtime (CLR).
Release of .NET Core
On November 12, 2014, Microsoft introduced .NET Core—an open-source, cross-platform successor to .NET Framework and released source code for the .NET Core Core CLR implementation and donated the code to the .NET Foundation under MIT license.
Release of .NET 5
In November 2020, Microsoft released .NET 5.0. The "Core" branding was abandoned and version 4.0 was skipped to avoid conflation with .NET Framework, of which the latest releases had all used 4.x versioning for all significant (non-bugfix) releases since 2010.
The newer versions are called .NET 6, .NET7,.NET8 and So on
The opensource version will be called as the .NET Platform and will be developed into the future while the older .NET framework 4.8.1 will remain installed on Windows platform for the foreseeable future
Installing .NET SDK on Your Computer
The .NET Software Development Kit (SDK) is a collection of tools, libraries, and resources provided by Microsoft to facilitate the development of applications targeting the .NET platform. It includes compilers, libraries, runtime environments, and other tools necessary for building, debugging, and deploying .NET applications.
The .NET SDK allows developers to create a wide range of applications, including desktop, web, mobile, cloud, and IoT applications, using programming languages such as C#, F#, and Visual Basic.NET. It also supports multiple operating systems, including Windows, macOS, and various Linux distributions like Ubuntu,Mint,Debian etc
You can go to the this link and download the required SDK for your Platform. Here I am down loading the Windows version of the .NET SDK and i will select the .NET 8 which is the one with the Long term support.
Download the executable and run it.
If you are on Linux system, check out the system specific Linux commands for your linux distribution.
If you are on Ubuntu Linux ,
- You can use this tutorial on how to install .NET SDK on Ubuntu Linux for Serial Port programming using VB.net
.NET SDK CLI Tools Command Structure
Once the .NET SDK is installed on your System (Windows/Linux/ MacOS X).You can now build your first .NET project using either C# or Visual Basic.NET using the .NET SDK Command line Tools.
The Command Line Interface command structure consists of the driver ("dotnet"), the command, and possibly command arguments and options.
The driver is named dotnet and has two responsibilities
- running an app
- executing a command like new or build.
So command may look like
dotnet new console
- here dotnet is the driver
- new is the command which the driver dotnet runs
- and console is the option telling which type of project to create
Creating a Simple Console Project Using .NET SDK
The .NET SDK command line provides a versatile set of tools for creating projects tailored to different environments and platforms. Using the dotnet new command, we can generate a wide range of project types, including
- Console applications,
- WinForms applications,
- WPF (Windows Presentation Foundation) applications,
- and Web applications.
Moreover, the .NET SDK is cross-platform, meaning you can use it on Windows, Linux, and macOS. This cross-platform capability allows developers to create and work on .NET projects regardless of their operating system.
Here we can use the dotnet new command to create a simple console project for beginners.
dotnet new console
Creating a console project using the .NET SDK will automatically default to using the C# language. This means that when you run the command to create a new console project in your current directory, it will be written in C#.
However, if you want to use other languages such as VB.NET or F#, you need to explicitly specify the language during project creation
Choosing a Programming Language to Develop Your .NET App
In the previous example we created a simple C# project using dotnet new command. Now what will we do if you want to use a different language line F# or VB.NET.
To select a specific language like C#,Visualbasic.net or F# while creating a project using .NET SDK CLI ,you should use the --language option.
--language option or --lang option allow the user to select which language to use in their project. Below are the valid options.
- C# for Csharp language
- F# for Fsharp Language
- VB for VisualBasic.NET
Here is the syntax for creating a VisualBasic.NET language console project
dotnet new console --lang VB
or
dotnet new console --language VB
this will create a Visualbasic.net console project on your disk as shown below
Here is the syntax for creating a F# language console project
dotnet new console --lang F#
or
dotnet new console --language F#
Project directory structure of F# is shown below
If nothing is specified the project will be a C# project
dotnet new console
you can also specify it explicitly
dotnet new console --language C#
Creating a WinForms Project Using .NET SDK
WinForms is a graphical user interface (GUI) framework included as part of the Microsoft .NET Framework and later .NET platforms. Developers can also extend the functionality of WinForms applications by writing event handlers in languages such as C# or Visual Basic .NET.
.NET SDK CLI allows you to create WinForm projects on Windows Platform.
Please note that although .NET platform supports WinForms, it is only available on Windows Platforms due to tight integration with Win32/64 API.
You may be able to run the Winforms applications on Linux using Wine Software.
To create a WinForms Project, type the following code into your command line.
dotnet new winforms -o output_directory_name
or
dotnet new winforms -o my_winforms_project
Creating a WPF Project Using .NET SDK
WPF or Windows Presentation Foundation. It's a graphical subsystem developed by Microsoft for rendering user interfaces in Windows-based applications. WPF is part of the .NET Framework and the later .NET platform. It provides a comprehensive set of features for building visually stunning and interactive user interfaces for desktop applications.
WPF uses Extensible Application Markup Language (XAML) as its markup language for defining user interfaces. XAML allows developers to create UI elements and define their properties using text based tools.
WPF applications are designed specifically for the Windows operating system and do not run natively on other platforms such as Linux or macOS
.NET SDK CLI allows you to create WPF projects using the below command. Replace the output directory name with the one you want.
dotnet new wpf -o wpf_output_directory_name
On running that command,.NET SDK will generate the following directory structure.
Using the .NET SDK CLI tools for WinForms or WPF development is feasible, but it may not be the most efficient approach, especially for graphical user interface (GUI) applications. it's generally more practical to use an IDE like Visual Studio for these types of applications.
Creating a Web Application Using ASP.NET Core With .NET SDK CLI
ASP.NET Core is an open-source modular web-application framework. It is a redesign of older ASP.NET which was part of the .NET framework. It runs on macOS, Linux, and Windows
You can create a New Web Application by opening your terminal and navigate to the directory where you want to create your project. Then, run the following command to create a new web application
dotnet new web -n WebApp
This command creates a new web application named "WebApp" using the ASP.NET Core template.
To run your web application. Please type
dotnet run
This command will build and run your ASP.NET Core web application. By default, it will listen on http://localhost:5000/.
How to Add References to Required .net Libraries Using .NET SDK
To add references to required .NET libraries using the .NET SDK, you typically use the dotnet add reference command.
This command allows you to specify the dependencies your project relies on, and it automatically updates the project file (e.g., .csproj) to include those dependencies
To add a reference to a NuGet package, you can use the dotnet add package command like this.
dotnet add package NugetPackageName
For example,
if you want to add a dapper reference to your local project. You can go to nuget.org and find the correct name of the package you want to download and then issue that as command
dotnet add package Dapper --version 2.1.35
Running Your .NET Project
Here we will learn to build our .NET application using .NET SDK command line tools and run it.
You can build your project using the command
dotnet build
When you run this command, it compiles the source code files in your project and produces executable files, libraries, or other artifacts based on the project type.
The dotnet build command also resolves dependencies specified in your project file (.csproj or .vbproj) and retrieves the necessary packages from NuGet repositories
You can now execute the executable by calling the run command like
dotnet run
here is the output for that
Create an Executable Using .NET SDK CLI
You can compile your .NET application into a single folder containing both the executable (.exe) file and its necessary dependencies (.dll) using the dotnet publish command available in the .NET SDK.
Go inside the folder containing your project and run the following command
dotnet publish
this will create the executable inside the folder publish along with the required .dll files.
As you can see from here that publish folder contains a lot of dll files and json files.You can create a single file executable using the -p:PublishSingleFile=true option.
You can find the detailed tutorial on creating a single file .NET platform application using the .NET SDK CLI here.