Generics in C, using type of a variable as parameter
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In C#, generic type arguments are chosen at compile time, not extracted from an arbitrary variable at runtime. That is why you cannot directly write code that says "take the type of this variable and plug it into a generic parameter" unless the compiler already knows the type or you drop down to reflection.
Generics Need Compile-Time Types
This works because the compiler knows the type argument:
In the second call, the compiler infers T from the argument type.
What C# does not let you do is treat a Type value as if it were a generic type argument in normal syntax.
typeof(x) Is Not a Generic Argument
People often want something like this:
That cannot work in normal generic syntax because t is a runtime value, while Echo<t> requires a compile-time type parameter.
This is the core rule to remember:
- generic syntax uses types known to the compiler
- '
System.Typevalues exist at runtime'
Those are related concepts, but they are not interchangeable in C# syntax.
Let the Compiler Infer the Generic Type
If you already have a value and you are calling a generic method directly, type inference is usually enough.
Notice the last line. The runtime object is an int, but the compile-time type of the variable is object, so inference chooses object.
Use Reflection When the Type Is Known Only at Runtime
If all you have is a runtime Type, reflection is the normal escape hatch.
This works, but it is slower, less readable, and more brittle than direct generic calls.
Sometimes Generics Are the Wrong Tool
If the real requirement is "handle values of different runtime types uniformly," then a non-generic design may be better:
- interfaces
- base classes
- pattern matching
- dictionaries keyed by
Type
Example with pattern matching:
This avoids pretending the problem is compile-time generic when it is really runtime dispatch.
Generic Types Can Still Use Type Values Indirectly
A common practical pattern is to map runtime Type objects to pre-built handlers.
This is often simpler than reflection-heavy generic invocation.
Common Pitfalls
The biggest mistake is confusing a runtime Type instance with a compile-time generic argument. They are not interchangeable in C# syntax.
Another issue is relying on generic type inference when the variable has already been widened to object. In that case the compiler can only infer object.
A third problem is reaching for reflection immediately when pattern matching or an interface-based design would be clearer and safer.
Summary
- Generic type parameters in C# must be known at compile time.
- A runtime
Typevalue cannot be used directly in normal generic syntax. - Let the compiler infer the generic type when possible.
- Use reflection only when the type is truly known only at runtime.
- If the problem is runtime dispatch, consider non-generic designs first.

