C# Story

Chapter #9 – Libraries

.NET Base Class Library, NuGet packages, common namespaces

9.0 Prologue

C# draws much of its power from the .NET Base Class Library (BCL) — a vast collection of types organized into namespaces. Rather than reinventing common functionality, C# programs import and use BCL types. This chapter surveys the most important namespaces and explains how additional libraries are obtained via NuGet.

9.1 The Base Class Library

The BCL is shipped with every .NET installation. Access any namespace with a using directive; the runtime loads the required assembly automatically.
Namespace Key types / purpose
System Object, String, Math, Console, DateTime, TimeSpan, Guid, Exception, Convert, Environment
System.Collections.Generic List<T>, Dictionary<K,V>, HashSet<T>, Queue<T>, Stack<T>, SortedSet<T>
System.Linq LINQ extension methods: Where, Select, OrderBy, GroupBy, Join, Aggregate, …
System.IO FileStream, StreamReader/Writer, MemoryStream, BinaryReader/Writer, Path, Directory, File
System.Text StringBuilder, Encoding (UTF-8/UTF-16/ASCII), RegularExpressions
System.Net.Http HttpClient, HttpRequestMessage, HttpResponseMessage
System.Threading Thread, Mutex, Monitor, Interlocked, ManualResetEvent, SemaphoreSlim
System.Threading.Tasks Task, Task<T>, Parallel, CancellationToken, TaskCompletionSource
System.Threading.Channels Channel<T>, ChannelReader, ChannelWriter (async producer-consumer)
System.Reflection Type, MethodInfo, PropertyInfo, Assembly (runtime type inspection)
System.Runtime.InteropServices P/Invoke, Marshal, GCHandle (native interop)
System.Diagnostics Stopwatch, Process, Debug, Trace, Activity (for observability)
System.Numerics BigInteger, Complex, Vector<T>, Matrix3x2, generic math interfaces
System.Memory Span<T>, ReadOnlySpan<T>, Memory<T>, MemoryPool<T>
System.Text.Json JsonSerializer, JsonDocument, Utf8JsonReader/Writer (zero-allocation JSON)
System.Xml.Linq XDocument, XElement, XAttribute (LINQ to XML)

9.2 NuGet — the .NET Package Manager

NuGet is the official package manager for .NET. Add a package to a project with: dotnet add package <PackageName> This updates the .csproj file with a <PackageReference> entry and downloads the package on the next build. The lock file packages.lock.json (opt-in) pins exact versions for reproducible builds. Widely used NuGet packages:
  • Newtonsoft.Json — JSON serialization (mature, widely used)
  • Microsoft.EntityFrameworkCore — ORM for databases
  • Serilog / NLog / log4net — structured logging
  • xUnit / NUnit / MSTest — unit testing frameworks
  • Moq / NSubstitute — mocking frameworks
  • FluentAssertions — readable test assertions
  • Polly — resilience and retry policies
  • AutoMapper — object-to-object mapping
  • MediatR — in-process messaging / CQRS

9.3 Project File and Global Usings

A .csproj file controls references, target framework, nullable analysis, and other build settings. With implicit usings (default in .NET 6+), many common namespaces are automatically available in every file without explicit using directives: <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net9.0</TargetFramework> <Nullable>enable</Nullable> <ImplicitUsings>enable</ImplicitUsings> </PropertyGroup> <ItemGroup> <PackageReference Include="Serilog" Version="4.*" /> </ItemGroup> </Project> Global using directives (C# 10+) let you declare a using once in a single file and have it apply across the entire project: global using System.Text.Json;

9.4 Epilogue

This chapter surveyed the .NET BCL and the NuGet package ecosystem. The next two chapters look closely at two particularly important library areas: streams and collections.

9.5 References

BCL API reference — Microsoft docs
NuGet.org — package repository
.NET project SDKs