The BCL map

The base class library is the part of .NET a C# program uses without installing anything: strings, lists, files, JSON, HTTP, threads, reflection. It is organised by namespace, and the namespaces are not arbitrary: one namespace is one area of the library, and the type you need is almost always in the area you would guess. This lesson counts the library and then maps the namespaces this course taught.

Counting the library

The program asks reflection for the public types of the core assembly, groups them by namespace and prints the twelve largest groups. Then it loads five framework assemblies by name and counts their types, and finally counts the assemblies the runtime has loaded:

using System.Reflection; Console.WriteLine($"runtime {Environment.Version}"); var core = typeof(object).Assembly; var namespaces = core.GetTypes() .Where(type => type.IsPublic) .GroupBy(type => type.Namespace ?? "(none)") .OrderByDescending(group => group.Count()) .ThenBy(group => group.Key) .Take(12); foreach (var group in namespaces) { Console.WriteLine($"{group.Key,-40}{group.Count(),5}"); } foreach (var name in new[] { "System.Runtime", "System.Collections", "System.Linq", "System.Text.Json", "System.Net.Http" }) { var assembly = Assembly.Load(name); Console.WriteLine($"{name,-22}{assembly.GetTypes().Length,5} types"); } Console.WriteLine($"assemblies loaded {AppDomain.CurrentDomain.GetAssemblies().Length}");

dotnet run
echo "status: $?"

runtime 10.0.15
System                                    253
System.Runtime.CompilerServices           129
System.Runtime.InteropServices            100
System.Reflection                          90
System.Threading                           65
System.Runtime.InteropServices.ComTypes    46
System.Numerics                            43
System.Globalization                       42
System.IO                                  41
System.Collections.Generic                 35
System.Text                                32
System.Runtime.Intrinsics.X86              31
System.Runtime            0 types
System.Collections       70 types
System.Linq             119 types
System.Text.Json        377 types
System.Net.Http         423 types
assemblies loaded 20
status: 0

The first line is the version of the runtime, not of the SDK: 10.0.15 is the shared framework that runs the program, and 10.0.303 is the SDK that built it. The counts are public types only, so a namespace whose types are internal or are all nested in another one does not appear.

The map

NamespaceWhat it holdsLessons
Systemthe fundamental types, Console, exceptions, Math, DateTime, Convert001-016, 059-062, 074-076
System.Collections.GenericList, Dictionary, HashSet, Queue, Stack, SortedDictionary021-024
System.LinqWhere, Select, GroupBy, Join and the query operators035-041
System.TextStringBuilder, Encoding018, 070
System.Text.JsonJsonSerializer, JsonDocument, the source generator071, 072, 094
System.IOFile, Directory, Path, Stream, StreamReader and StreamWriter063, 069, 070, 077
System.Net.HttpHttpClient and the HTTP message types098
System.ThreadingThread, locks, timers, CancellationToken, SemaphoreSlim081, 083-085, 090
System.Threading.ChannelsChannel, the writer and the reader087, 128-132
System.Threading.TasksTask, Parallel, TaskCompletionSource079, 080, 088
System.GlobalizationCultureInfo, the number and date formats075, 076
System.NumericsBigInteger, complex numbers, the vector types014 mentions the range of the integers
System.ReflectionType, MethodInfo, the attributes of a type092, 093
System.Runtime.InteropServicesDllImport, StructLayout, Marshal099, 100
System.DiagnosticsStopwatch, Debug, Process, Activity078, 102, 109
System.Buffers and System.MemorySpan, Memory, ArrayPool020, 105
System.Security.CryptographySHA256, RandomNumberGenerator, the AES typesnot in this course
System.Runtime.CompilerServicesthe attributes the compiler itself reads093

The rows with two namespaces are one subject: a span is a type from System and a buffer pool is a type from System.Buffers. The cryptography row is the one area of the library this course does not teach, and it is a deliberate omission: a hash or a cipher needs a lesson of its own per algorithm, and the API is stable enough that the reference is the better teacher.

A facade with no types

The line System.Runtime 0 types looks like a mistake and is not one. System.Runtime is a facade assembly: it carries the metadata that says which types belong to the framework, and the types themselves live in System.Private.CoreLib. The same is true of several assemblies of the shared framework, and it is why the count of the core assembly in the first table is the real one.

What to read next

The reference for every type in the tables is the .NET API browser, which is organised by exactly these namespaces. The language reference is separate and smaller: the C# specification and the release notes of the version the project names, which lesson 004 and the LangVersion property tie together.

A REPL would be the shortest way to explore the library, and this container does not have one for C#: dotnet fsi is F#, as lesson 012 measured, and there is no csi and no dotnet script. The shortest experiment is a console project, which is one command and one file, and every lesson of this course is such an experiment.

The course ends here. The language and the library are the two halves of what a C# program is; the frameworks of the previous lesson are the third, and they are worth a course each.

Exercises

  1. Change the number twelve in Take(12) to thirty and find the namespace of the types you have used but never named.
  2. Count the types of one namespace only, System.Collections, and compare the number with the table.
  3. Print the assembly that implements typeof(List<int>) and explain why it is not the assembly of the program.
  4. Add System.Security.Cryptography to the list of assemblies and find out whether loading it works without a reference.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Which assembly holds most of the types of the base class library?

    Answer

    System.Private.CoreLib, behind the System.Runtime facade

    The measured System.Runtime row has zero types.

  2. Which namespace holds List, Dictionary and HashSet?

    Answer

    System.Collections.Generic

    The measured count of that namespace is 35 public types.

  3. What does this program print? Console.WriteLine(typeof(List<int>).Namespace);

    Answer

    System.Collections.Generic

    The generic collections live in their own namespace.

  4. Which namespace holds Channel and its writer and reader?

    Answer

    System.Threading.Channels

    Lessons 087 and 128 to 132 use it.

  5. What does this program print? Console.WriteLine(typeof(System.Text.Json.JsonSerializer).Namespace);

    Answer

    System.Text.Json

    That namespace holds 377 public types in the measured count.

  6. Why does Assembly.Load("System.Runtime") succeed and report no types?

    Answer

    It is a facade: it names the types and another assembly implements them

    The program printed 0 types for that assembly and 20 assemblies loaded.

  7. Which namespace holds DllImport and StructLayout?

    Answer

    System.Runtime.InteropServices

    Lessons 099 and 100 use it for P/Invoke and marshalling.

  8. What does this program print? Console.WriteLine(typeof(Console).Namespace);

    Answer

    System

    Console is one of the 253 public types of System in the measured count.

  9. Which lesson measured that dotnet fsi is F# and there is no C# REPL?

    Answer

    Lesson 012

    The CLI lesson lists what the image has and what it does not.

  10. Which area of the library does this course deliberately not teach?

    Answer

    System.Security.Cryptography

    A hash or a cipher needs its own lesson per algorithm.

The code on this page was built with the .NET SDK 10.0.303 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev