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
| Namespace | What it holds | Lessons |
|---|---|---|
| System | the fundamental types, Console, exceptions, Math, DateTime, Convert | 001-016, 059-062, 074-076 |
| System.Collections.Generic | List, Dictionary, HashSet, Queue, Stack, SortedDictionary | 021-024 |
| System.Linq | Where, Select, GroupBy, Join and the query operators | 035-041 |
| System.Text | StringBuilder, Encoding | 018, 070 |
| System.Text.Json | JsonSerializer, JsonDocument, the source generator | 071, 072, 094 |
| System.IO | File, Directory, Path, Stream, StreamReader and StreamWriter | 063, 069, 070, 077 |
| System.Net.Http | HttpClient and the HTTP message types | 098 |
| System.Threading | Thread, locks, timers, CancellationToken, SemaphoreSlim | 081, 083-085, 090 |
| System.Threading.Channels | Channel, the writer and the reader | 087, 128-132 |
| System.Threading.Tasks | Task, Parallel, TaskCompletionSource | 079, 080, 088 |
| System.Globalization | CultureInfo, the number and date formats | 075, 076 |
| System.Numerics | BigInteger, complex numbers, the vector types | 014 mentions the range of the integers |
| System.Reflection | Type, MethodInfo, the attributes of a type | 092, 093 |
| System.Runtime.InteropServices | DllImport, StructLayout, Marshal | 099, 100 |
| System.Diagnostics | Stopwatch, Debug, Process, Activity | 078, 102, 109 |
| System.Buffers and System.Memory | Span, Memory, ArrayPool | 020, 105 |
| System.Security.Cryptography | SHA256, RandomNumberGenerator, the AES types | not in this course |
| System.Runtime.CompilerServices | the attributes the compiler itself reads | 093 |
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.
- A reference to a type is resolved through the facade to the assembly that implements it, which is how a library can target an older framework and run on a newer one.
- Assembly.Load by name worked for all five assemblies because they are in the shared framework of the running app. A package that is not referenced is not there to load, which is the restore failure of lesson 094 in another form.
- Twenty assemblies were loaded at that point: the core library, the runtime services, the JSON and HTTP stacks the program touched, and the app itself. A console program that touches nothing loads fewer.
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
- Change the number twelve in Take(12) to thirty and find the namespace of the types you have used but never named.
- Count the types of one namespace only, System.Collections, and compare the number with the table.
- Print the assembly that implements typeof(List<int>) and explain why it is not the assembly of the program.
- 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.
-
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.
-
Which namespace holds List, Dictionary and HashSet?
Answer
System.Collections.Generic
The measured count of that namespace is 35 public types.
-
What does this program print?
Console.WriteLine(typeof(List<int>).Namespace);Answer
System.Collections.Generic
The generic collections live in their own namespace.
-
Which namespace holds Channel and its writer and reader?
Answer
System.Threading.Channels
Lessons 087 and 128 to 132 use it.
-
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.
-
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.
-
Which namespace holds DllImport and StructLayout?
Answer
System.Runtime.InteropServices
Lessons 099 and 100 use it for P/Invoke and marshalling.
-
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.
-
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.
-
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