In C#, why do `typeof(T)` and `value is List<T>` work inside a generic method, and how does `typeof(List<>)` differ from `typeof(List<int>)`?
answer
- type arguments survive compilation
- each closed type knows its argument
- List<string> is not List<object>
- generic type definition versus closed type
- GetGenericTypeDefinition and GetGenericArguments
basics
~20 sC# generics are reified: each closed type such as List<string> carries its type argument at run time, so typeof(T) and is List<T> see the real argument. typeof(List<>) is the open generic type definition; typeof(List<int>) is one closed type built from it.
solid answer
~40 sThe CLR keeps type parameters in metadata and builds a distinct closed constructed type for each set of arguments, so inside a generic method `typeof(T)` returns the actual argument's `Type`, `value is List<T>` is a real run-time test, and `new List<T>()` really is a `List<string>` when `T` is `string`. That also makes `List<string>` and `List<object>` unrelated types: a `List<string>` fails `is List<object>`. `typeof(List<>)` names the generic type definition (``List`1``, `IsGenericTypeDefinition` true), while `typeof(List<int>)` is a closed type whose `GetGenericTypeDefinition()` returns that same definition and whose `GetGenericArguments()` holds `int`. Unbound names are legal only in `typeof` and, since C# 14, in `nameof`; anywhere else they are CS7003.
code
csharp · 14 linesobject boxed = new List<string> { "ping" };
Console.WriteLine(Inspect<string>(boxed));
Console.WriteLine(Inspect<object>(boxed));
Type open = typeof(List<>);
Type closed = boxed.GetType();
Console.WriteLine($"{open.Name} {open.IsGenericTypeDefinition}");
Console.WriteLine($"{closed} {closed.IsConstructedGenericType}");
Console.WriteLine(closed.GetGenericTypeDefinition() == open);
Console.WriteLine(closed.GetGenericArguments()[0].Name);
Console.WriteLine(nameof(List<>)); // C# 14: unbound generic types in nameof
static string Inspect<T>(object value) =>
$"T = {typeof(T).Name}, is List<T>: {value is List<T>}";go deeper
Recall that C# keeps type arguments at run time: typeof(T) gives the real type and List<string> is a different type from List<object>.
Explain open versus closed types with typeof(List<>), GetGenericTypeDefinition and GetGenericArguments, and say where an unbound generic name is legal, including nameof in C# 14.
Use reification deliberately: match closed types against definitions in registration or dispatch code, and catch tests like is List<object> that can never succeed for a List<string>.
When the interviewer contrasts C# with erased generics, frame reification as a platform trade-off: run-time type tests and per-type statics, paid for with a distinct runtime type for every instantiation.
## What "reified" means for C# generics **Reification** means the type arguments of a generic type or method are real at run time, not just a compile-time check. When the C# compiler emits a generic class or method, the intermediate language keeps its **type parameters** in metadata. When code uses `List<string>`, the CLR creates the **closed constructed type** `List<string>`, a type of its own that knows its argument is `string`. The C# programming guide puts it this way: when a generic class is instantiated, "reflection can query it at run time and both its actual type and its type parameter can be ascertained". Inside a generic method, the type argument is therefore available to the method body: - **`typeof(T)`** returns the `System.Type` of the actual argument, such as `System.String` or `System.Int32`. - **`value is T`** and **`value is List<T>`** are genuine run-time type tests against that argument, and `value is T typed` also declares a variable of the argument's type when the test passes. - **`new List<T>()`** creates a `List<string>` when `T` is `string`, and `GetType()` on it says so. - **`new T[16]`** is a real `string[]`, and **`default(T)`** is the default of the actual argument. A platform that erases type arguments (Java is the usual comparison) cannot express most of these tests; in C# they are ordinary code. ## Distinct closed types, distinct identities Because the argument is part of the type's identity, `List<string>` and `List<object>` are **different types** at run time: 1. `typeof(List<string>) == typeof(List<object>)` is `false`. 2. A `List<string>` stored in an `object` variable fails `is List<object>`, which is why `Inspect<object>` in the snippet prints `False`. 3. A class type argument is not "rounded down" to `object`: `Inspect<string>` prints `True` precisely because the closed type remembers `string`. How the JIT shares machine code among reference-type instantiations is a runtime implementation detail; it never changes what `typeof(T)` or `is` report. ## Open and closed types in `typeof` C# lets you name a generic type with no arguments, an **unbound generic name**, in a few places. The result is the **generic type definition**, the open template from which closed types are made: | Expression | Result | |---|---| | `typeof(List<>)` | the definition, `Name` is ``List`1``, `IsGenericTypeDefinition` is `true` | | `typeof(Dictionary<,>)` | the two-parameter definition, ``Dictionary`2``; commas give the arity | | `typeof(List<int>)` | a closed type, `IsConstructedGenericType` is `true` | | `typeof(List<int>).GetGenericTypeDefinition()` | the same `Type` object as `typeof(List<>)` | | `typeof(List<int>).GetGenericArguments()` | an array holding `typeof(int)` | | `nameof(List<>)` (C# 14) | the string `"List"` | The backtick and number in ``List`1`` are the CLR's metadata name: the number is the count of type parameters. ## Where unbound names are allowed - **`typeof(...)`** has always accepted them: `typeof(List<>)`, `typeof(Dictionary<,>)`. - **`nameof(...)`** accepts them **since C# 14**: `nameof(List<>)` evaluates to `"List"`. Compiled with `LangVersion` 13, the SDK reports **CS9260**, "Feature 'unbound generic types in nameof operator' is not available in C# 13.0". - **Anywhere else** they are an error: `List<> items = null!;` gives **CS7003**, "Unexpected use of an unbound generic name". A partly bound name such as `typeof(Dictionary<int,>)` does not even parse (CS1031, "Type expected"). ## What this is used for The most common practical use is **matching a closed type against a generic definition**: ```csharp static bool IsListType(Type type) => type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List<>); ``` On the game server's message bus, a dispatcher holding messages as `object` can hand one to a `MessageChannel<TMessage>` only when `message is TMessage typed` succeeds, a test against the real argument. Registration code, test helpers and dispatchers also use the definition check above to find, say, every implementation of a handler interface whatever its message type, then read the message type with `GetGenericArguments()`. Building a closed type from a definition at run time, and invoking members through it, is reflection proper and belongs to the reflection material; the point here is that the information is there to read, because C# generics are reified. ## Summary Type arguments survive compilation. `typeof(T)`, `is T`, `new T[]` and `new List<T>()` all see the real argument; `List<string>` and `List<object>` are unrelated closed types; `typeof(List<>)` names the open definition that every `List<X>` comes from, and C# 14 lets `nameof` take the same unbound form.
- How do you check whether an object is some `List<X>` without knowing `X`?Take its runtime type and compare definitions: `var t = obj.GetType(); bool isList = t.IsGenericType && t.GetGenericTypeDefinition() == typeof(List<>);`. Then `t.GetGenericArguments()[0]` tells you `X`. A pattern such as `obj is List<object>` will not do, because `List<string>` and `List<object>` are distinct closed types.
- What changed for `nameof` with generic types in C# 14?Before C# 14, `nameof` required a closed type such as `nameof(List<int>)`, which evaluates to `"List"`. C# 14 accepts an unbound generic type, so `nameof(List<>)` gives the same string without inventing a type argument. Compiling it with an older `LangVersion` fails with CS9260.
- Does `typeof(T)` inside a generic method reflect the argument at run time or at compile time?Run time. The method body is compiled once against `T`, and `typeof(T)` returns the `Type` of whatever argument the call closed it with, including a `T` passed through from another generic caller. That is why `Describe<T>()` can print `Int32` for one call and `String` for the next.
- C# erases type arguments so typeof(T) returns object
- a List<string> passes an is List<object> test
- typeof(List<>) and typeof(List<int>) are the same Type
- List<> can be used as a variable's declared type
- nameof(List<>) has always been allowed
Assumes C# 14 for nameof(List<>), which earlier versions reject with CS9260; typeof, is and GetGenericTypeDefinition behave the same in every supported version.