.NET Platform, CLR, Assemblies & NuGet Masterclass
Welcome to Phase 11 (Chapter 30): C# .NET Platform, CLR, Assemblies & NuGet Masterclass! Understanding the .NET platform architecture is essential for building enterprise-grade applications. In this chapter, we explore the Common Language Runtime (CLR), managed execution model, Assemblies (.dll/.exe), NuGet package management, project configuration, environment variables, logging with ILogger, Dependency Injection in .NET host, and application lifecycle management.
NuGet is the official .NET package manager hosting over 350,000 open-source libraries. You can add packages using the dotnet add package CLI or editing the .csproj file directly.
# Add NuGet package
dotnet add package Newtonsoft.Json
dotnet add package Serilog.AspNetCore
# List installed packages
dotnet list package
# Remove a package
dotnet remove package Newtonsoft.Json
# Restore all packages from .csproj references
dotnet restore
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
</Project>
using Microsoft.Extensions.Configuration;
// appsettings.json configuration binding
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true)
.AddEnvironmentVariables()
.Build();
string? connectionString = config["ConnectionStrings:DefaultConnection"];
Console.WriteLine($"Connection: {connectionString ?? "Not configured"}");
// Reading system environment variable
string? aspNetCoreEnv = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
Console.WriteLine($"Environment: {aspNetCoreEnv ?? "Development"}");
Q1: What is the difference between an Assembly and a NuGet package?
An Assembly is a compiled .dll or .exe output file containing CIL bytecode. A NuGet package is a versioned bundle (.nupkg) containing one or more assemblies, metadata, and dependency information distributed via NuGet.org.
Q2: What does ImplicitUsings enable in .csproj?
It automatically adds global using directives for common namespaces like System, System.Linq, System.IO, and System.Collections.Generic across all source files without requiring manual imports.