Skip to main content

Practice · 2 of 2

Generic specialization at runtime

CHALLENGE
Difficulty: advanced+25 XP

Value-type generics get per-type specialization; reference types share one instantiation. Prove the observable part: implement static int OpenGenericCount() returning the number of open generic type definitions reachable via typeof(Dictionary<,>) (0 if it is constructed, 1 if open) — implement it as: typeof(Dictionary<,>).IsGenericTypeDefinition ? 1 : 0. Then implement static bool SameRuntimeType<A, B>() => typeof(A) == typeof(B) and the test checks SameRuntimeType<List<int>, List<int>>() is true while SameRuntimeType<List<int>, List<string>>() is false.

Back to lesson: Practice: CLR internals drills