Generic classes, methods and interfaces, constraints, covariance, contravariance and invariance, generic performance and open generic registration in DI.
-
What are generic classes in C#? Why do generics exist, how do you declare and close a generic type, and how do static fields behave for each constructed type such as Cache<int> and Cache<string>?
Generic classes: type parameters, open versus closed types, reification, per-type static state and when a class should be generic.
Junior
Theory
Generics
Β· π 3 follow-ups
-
How do generic methods work in C#? Explain how the compiler infers type arguments, when inference fails, and when you would make a method generic instead of the whole class.
Generic methods: declaration, type inference rules and limits, generic methods inside non-generic types, and design guidance.
Junior
Coding
Generics
Β· π 3 follow-ups Β· π 1 real-world
-
What are generic interfaces in C#? Explain why IEquatable<T>, IComparable<T> and IEnumerable<T> exist alongside their non-generic versions, how a type implements several closed versions of one interface, and how to design your own generic interface such as IRepository<T>.
Generic interfaces: type-safe contracts, avoiding boxing, implementing multiple closed interfaces, explicit implementation and designing generic abstractions.
Intermediate
Theory
Generics
Β· π 3 follow-ups Β· π 1 real-world
-
What generic constraints does C# support? Explain class, class?, struct, unmanaged, notnull, new(), base-class, interface, naked type, Enum and Delegate constraints, and the C# 13 allows ref struct anti-constraint, with an example of when you need each.
Every generic constraint in C# 14, what each enables, the order rules, and how constraints affect performance and API design.
Mid-Level
Theory
Generics
Β· π 3 follow-ups Β· π 1 real-world
-
What is covariance in C# generics? Explain the out modifier, why IEnumerable<string> converts to IEnumerable<object> but List<string> does not convert to List<object>, which BCL types are covariant, and the rules a covariant type parameter must follow.
Generic covariance: out T, output-only positions, reference conversions only, BCL examples and designing covariant interfaces and delegates.
Mid-Level
Theory
Generics
Β· π 3 follow-ups Β· π 1 real-world
-
What is contravariance in C# generics? Explain the in modifier, why an IComparer<object> or Action<object> can be used where IComparer<string> or Action<string> is expected, which BCL types are contravariant, and the gotcha with combining variant delegates.
Generic contravariance: in T, input-only positions, BCL comparers and delegates, and the run-time failure when combining variance-converted delegates.
Mid-Level
Theory
Generics
Β· π 2 follow-ups Β· π 1 real-world
-
A colleague cannot pass a List<Dog> to a method that takes List<Animal>, cannot assign Dictionary<string, List<string>> to IDictionary<string, IEnumerable<string>>, and cannot return Task<Dog> where Task<Animal> is expected. Explain invariance and how you would fix each case.
Generic invariance: why most generic types are invariant, how to diagnose CS1503/CS0266 conversion errors, and the fixes (covariant interfaces, generic methods with constraints, projection).
Mid-Level
Debugging
Generics
Β· π 3 follow-ups Β· π 1 real-world
-
How do generics perform in .NET? Explain how the JIT shares code for reference-type instantiations and specialises it for value types, what that means for typeof(T) checks, new T(), generic virtual methods and generic static caches, and how generics affect Native AOT size.
Generic performance internals: canonical shared code (__Canon), value-type specialisation, runtime generic dictionaries, constant folding of typeof(T), boxing avoidance and AOT code size.
Senior
Theory
Generics
Β· π 3 follow-ups Β· π 1 real-world
-
How do you register and resolve open generic types in .NET dependency injection, for example services.AddScoped(typeof(IRepository<>), typeof(Repository<>))? Explain how the container closes the type, how closed registrations override it, lifetimes, decorators and the limits of Microsoft.Extensions.DependencyInjection.
Open generic registration in Microsoft DI: how resolution closes types, precedence over closed registrations, IEnumerable resolution, decorators with Scrutor, validation and AOT limits.
Senior
Architecture
Generics
Β· π 3 follow-ups Β· π 1 real-world