Skip to content
DevNursery.com - New Web Developer Docs
GitHub

The .NET Environment

A Comprehensive Overview of .NET

What is .NET?

.NET is a versatile and popular software development platform created by Microsoft. It provides a comprehensive ecosystem for building a wide range of applications, from web and desktop applications to cloud-based services and mobile apps. .NET combines a runtime environment, a rich class library, and various development tools to streamline application development across different platforms.

The .NET Development Ecosystem:

The .NET development ecosystem consists of several key components:

.NET Runtime (Common Language Runtime - CLR): The CLR is the heart of the .NET platform. It provides the execution environment for managed code written in .NET languages, including memory management, security, and exception handling.

.NET Class Library: The .NET Framework (now called .NET Core and .NET 5+) includes a vast class library that offers a wide range of pre-built functions and components for common programming tasks. This library simplifies development and promotes code reuse.

Integrated Development Environments (IDEs): Developers use Visual Studio and Visual Studio Code, Microsoft’s integrated development environments, for writing, testing, and debugging .NET applications. These IDEs offer powerful tools for creating various types of projects.

Languages: .NET supports multiple programming languages, including C#, F#, VB.NET, and more. Developers can choose the language that best suits their application requirements and personal preferences.

NuGet: NuGet is a package manager for .NET that allows developers to easily add third-party libraries, components, and dependencies to their projects.

ASP.NET and ASP.NET Core: These frameworks are used for building web applications and web services. ASP.NET Core, in particular, is known for its cross-platform capabilities.

Entity Framework: A popular Object-Relational Mapping (ORM) framework that simplifies database access and management in .NET applications.

Xamarin: Xamarin is a framework for building cross-platform mobile applications for iOS, Android, and Windows using .NET languages. It allows code sharing between different platforms.

History of .NET:

.NET was first introduced by Microsoft in the early 2000s as the .NET Framework, primarily focused on Windows desktop and web application development.

In 2016, Microsoft introduced .NET Core, a modular, cross-platform version of .NET, which evolved into .NET 5 and later .NET 6, a unified platform supporting a wide range of application types on multiple platforms.

The goal of unification was to provide a consistent development experience for developers targeting Windows, Linux, macOS, and other platforms.

What .NET Provides Developers:

.NET offers several advantages to developers:

Cross-Platform Development: .NET 5 and .NET 6 enable cross-platform development, allowing developers to create applications that run on Windows, Linux, and macOS.

Language Choice: Developers can choose from multiple languages, including C#, F#, and VB.NET, to write .NET code based on their familiarity and project requirements.

Rich Ecosystem: The extensive class library, tooling, and frameworks available within .NET streamline development and reduce the need for third-party dependencies.

Performance: .NET’s Just-In-Time (JIT) compilation and runtime optimizations ensure that applications built with .NET are efficient and perform well.

Security: .NET includes built-in security features, including code access security, encryption libraries, and security protocols.

Community and Support: A large and active .NET developer community provides resources, tutorials, and support, making it easier for developers to learn and grow their skills.

Languages Supported by .NET:

.NET supports multiple programming languages, including but not limited to:

  • C#: The most widely used language for .NET development.

  • F#: A functional-first language that runs on .NET and is known for its concise and expressive syntax.

  • VB.NET: Visual Basic .NET, a language with a long history in the Microsoft ecosystem.

  • Others: .NET is designed to be extensible, and there are efforts to support additional languages and dialects.

In conclusion, .NET is a versatile and powerful development platform that supports a wide array of application types, offers multiple programming languages, and provides a rich set of tools and libraries. Its evolution from the .NET Framework to .NET 5 and .NET 6 reflects Microsoft’s commitment to making .NET a modern and cross-platform development ecosystem.

Syntax of .NET Languages

Hello World in C#, F#, and VB.NET:

C#:

using System;

class Program
{
    static void Main()
    {
        Console.WriteLine("Hello, World!");
    }
}

F#:

open System

[<EntryPoint>]
let main argv =
    Console.WriteLine("Hello, World!")
    0 // Return an integer exit code

VB.NET:

Imports System

Module Module1
    Sub Main()
        Console.WriteLine("Hello, World!")
    End Sub
End Module

Declaring and Using Variables:

C#:

int age = 30;
string name = "John";
double salary = 50000.50;

F#:

let age = 30
let name = "John"
let salary = 50000.50

VB.NET:

Dim age As Integer = 30
Dim name As String = "John"
Dim salary As Double = 50000.50

Conditionals (if-else):

C#:

int age = 25;
if (age < 18)
{
    Console.WriteLine("You are a minor.");
}
else
{
    Console.WriteLine("You are an adult.");
}

F#:

let age = 25
if age < 18 then
    printfn "You are a minor."
else
    printfn "You are an adult."

VB.NET:

Dim age As Integer = 25
If age < 18 Then
    Console.WriteLine("You are a minor.")
Else
    Console.WriteLine("You are an adult.")
End If

Loops (for loop):

C#:

for (int i = 0; i < 5; i++)
{
    Console.WriteLine(i);
}

F#:

for i in 0 .. 4 do
    printfn "%d" i

VB.NET:

For i As Integer = 0 To 4
    Console.WriteLine(i)
Next

Arrays and Key/Value Pairs:

C#:

// Arrays
int[] numbers = { 1, 2, 3, 4, 5 };

// Key/Value Pairs (using Dictionary)
using System.Collections.Generic;
Dictionary<string, int> ages = new Dictionary<string, int>();
ages.Add("Alice", 25);
ages.Add("Bob", 30);

F#:

// Arrays
let numbers = [| 1; 2; 3; 4; 5 |]

// Key/Value Pairs (using Map)
let ages = Map.ofList [ ("Alice", 25); ("Bob", 30) ]

VB.NET:

' Arrays
Dim numbers() As Integer = {1, 2, 3, 4, 5}

' Key/Value Pairs (using Dictionary)
Dim ages As New Dictionary(Of String, Integer)()
ages.Add("Alice", 25)
ages.Add("Bob", 30)

Functions:

C#:

int Add(int a, int b)
{
    return a + b;
}

F#:

let add a b =
    a + b

VB.NET:

Function Add(ByVal a As Integer, ByVal b As Integer) As Integer
    Return a + b
End Function

.NET CLI

The .NET CLI (Command-Line Interface) is a powerful tool for managing .NET projects, building, and running .NET applications across different platforms. It’s especially handy for developers who prefer working from the command line or for automating tasks in build pipelines. Here’s a discussion of how to create, compile, and run C#, F#, or VB.NET projects using the .NET CLI:

Creating a New .NET Project:

You can create new C#, F#, or VB.NET projects using the .NET CLI with the dotnet new command followed by the appropriate project template. Here are examples for each language:

C#:

dotnet new console -n MyCSharpApp

F#:

dotnet new console -lang F# -n MyFSharpApp

VB.NET:

dotnet new console -lang VB -n MyVBNetApp

In these examples:

  • dotnet new is the command to create a new project.
  • console specifies that you’re creating a console application.
  • -n specifies the project name, which you can replace with your desired project name.

Compiling Your Project:

Once you’ve created your project, you can use the dotnet build command to compile it.

Navigate to the project’s root directory (where the .csproj, .fsproj, or .vbproj file is located) and run:

dotnet build

This command will compile your project and produce the necessary binary files in the project’s output directory.

Running Your Project:

To run your compiled project, you can use the dotnet run command. Navigate to the project’s root directory and execute:

dotnet run

This command will execute the main entry point of your application (e.g., Program.cs for C#), and you’ll see the output in the terminal.

Additional Notes:

If you want to specify a different configuration (e.g., Debug or Release) or target framework, you can do so with appropriate flags. For example:

dotnet build -c Release
dotnet run -f netcoreapp3.1

The .NET CLI provides a wide range of commands for managing dependencies, publishing applications, and more. You can explore these commands by running dotnet —help or dotnet —help for specific command details.

Make sure you have the .NET SDK installed on your system before using the .NET CLI. You can download it from the official .NET website.

The examples above are for creating and running console applications. You can use different project templates and commands for other types of .NET projects (e.g., web applications, class libraries).

CLI Commands Chart

CommandPurposeSyntaxExample(s)
dotnet newCreate a new .NET project or filedotnet new <template> [-n <name>]dotnet new console -n MyCSharpApp
dotnet buildBuild a .NET project`dotnet build [-c—configuration ]`
dotnet runRun a .NET projectdotnet run [--project <path>]dotnet run
dotnet restoreRestore project dependenciesdotnet restoredotnet restore
dotnet publishPublish a .NET project for deployment`dotnet publish [-c—configuration ]`
dotnet testRun unit tests in a .NET projectdotnet test [--filter <filter>]dotnet test
dotnet add referenceAdd a reference to a .NET projectdotnet add reference <project>dotnet add reference MyLibrary.csproj
dotnet add packageAdd a NuGet package to a .NET projectdotnet add package <package>dotnet add package Newtonsoft.Json
dotnet publishPublish a .NET project for deployment`dotnet publish [-c—configuration ]`
dotnet ef migrations addCreate a new EF Core migrationdotnet ef migrations add <name>dotnet ef migrations add InitialMigration
dotnet ef database updateApply pending EF Core migrationsdotnet ef database updatedotnet ef database update
dotnet nuget pushPush a NuGet package to a feeddotnet nuget push <package> -s <source>dotnet nuget push MyPackage.1.0.0.nupkg -s https://nuget.example.com
dotnet restoreRestore project dependenciesdotnet restoredotnet restore
dotnet cleanClean the output of a .NET projectdotnet cleandotnet clean
dotnet packCreate a NuGet package from a projectdotnet pack [--output <path>]dotnet pack -o ./nupkgs
dotnet slnManage .NET solutions (create, add, remove)dotnet sln <solution> [command]dotnet sln MySolution.sln add MyProject.csproj
dotnet add referenceAdd a reference to a .NET projectdotnet add reference <project>dotnet add reference MyLibrary.csproj
dotnet add packageAdd a NuGet package to a .NET projectdotnet add package <package>dotnet add package Newtonsoft.Json
dotnet remove referenceRemove a reference from a .NET projectdotnet remove reference <project>dotnet remove reference MyLibrary.csproj
dotnet remove packageRemove a NuGet package from a .NET projectdotnet remove package <package>dotnet remove package Newtonsoft.Json
dotnet list referenceList project referencesdotnet list referencedotnet list reference
dotnet list packageList NuGet package references in a projectdotnet list packagedotnet list package
dotnet ef migrations addCreate a new EF Core migrationdotnet ef migrations add <name>dotnet ef migrations add InitialMigration
dotnet ef migrations removeRemove the last EF Core migrationdotnet ef migrations removedotnet ef migrations remove
dotnet ef database updateApply pending EF Core migrationsdotnet ef database updatedotnet ef database update
dotnet ef database dropDrop the databasedotnet ef database dropdotnet ef database drop
dotnet nuget pushPush a NuGet package to a feeddotnet nuget push <package> -s <source>dotnet nuget push MyPackage.1.0.0.nupkg -s https://nuget.example.com
dotnet nuget list sourceList configured NuGet package sourcesdotnet nuget list sourcedotnet nuget list source
dotnet nuget add sourceAdd a new NuGet package sourcedotnet nuget add source -n <name> -u <url>dotnet nuget add source -n MyFeed -u https://myfeed.example.com
dotnet nuget remove sourceRemove an existing NuGet package sourcedotnet nuget remove source -n <name>dotnet nuget remove source -n MyFeed

Working with NuGet: Managing Packages in .NET

NuGet is a package manager for .NET that simplifies the process of adding, updating, and managing external libraries, dependencies, and tools in your .NET projects. It is an essential tool for .NET developers, as it streamlines the integration of third-party components and ensures that your projects are built with the correct versions of libraries and packages.

What is NuGet?

NuGet is a package manager that helps you discover, install, and manage packages in your .NET projects. These packages can include libraries, frameworks, SDKs, tools, and other resources that enhance your project’s functionality and save you time by avoiding manual downloads and configurations.

How to Install Packages with NuGet:

Using Visual Studio (Windows):

  1. Open your project in Visual Studio. Right-click on your project in the Solution Explorer.

  2. Select “Manage NuGet Packages.”

  3. In the NuGet Package Manager window, search for the package you want to install.

  4. Click the “Install” button next to the package you want.

  5. Confirm any prompts to install dependencies.

Using Visual Studio Code (Cross-Platform):

  1. Open your project in Visual Studio Code.
  2. Open the integrated terminal.
  3. Use the dotnet add package command to install a package, for example:
dotnet add package PackageName

Using .NET CLI (Cross-Platform):

  1. Open a command prompt or terminal.
  2. Navigate to your project directory.
  3. Use the dotnet add package command to install a package, for example:
dotnet add package PackageName

Where are Packages Tracked in the Project:

When you install packages with NuGet, the package references and related information are tracked in your project files. The location and format of these references may vary depending on the project type:

For .NET Framework Projects (e.g., Windows Forms, WPF):

Packages are typically tracked in a packages.config file located at the project’s root or within a specific project folder.

Example packages.config file:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="PackageName" version="1.0.0" targetFramework="net472" />
</packages>

For .NET Core and .NET 5+ Projects:

Package references are stored in the project file itself (e.g., .csproj or .fsproj).

Example .csproj file:

<ItemGroup>
  <PackageReference Include="PackageName" Version="1.0.0" />
</ItemGroup>

These references include the package name, version, and other metadata required to restore and use the package in your project. NuGet takes care of downloading and managing the packages in a central cache on your system, ensuring that dependencies are resolved correctly.

In summary, NuGet is an indispensable tool for .NET developers that simplifies package management by providing a straightforward way to add, update, and manage external libraries and dependencies in your projects. Package references are typically tracked in project-specific configuration files or directly in project files, making it easy to manage and share dependencies across your development team.