Variance only works for reference-types (or there is an identity conversion). It is not known that TBase
is reference type, unless you add : class
:
public void Foo<TBase>() where TBase : class, IBase
since I could write a:
public struct Evil : IBase {}
More Related Contents:
- In C#, why can’t a List object be stored in a List variable
- vs in Generics
- c# covariant return types utilizing generics
- Why cannot IEnumerable be cast as IEnumerable?
- T must be contravariantly valid
- Why can’t I cast from a List to List?
- Why does C# (4.0) not allow co- and contravariance in generic class types?
- Why can’t I assign a List to a List?
- KeyValuePair with generic values [closed]
- Remove duplicates from a List in C#
- How to dynamically create generic C# object using reflection? [duplicate]
- Why isn’t there generic variance for classes in C# 4.0?
- Generics and casting – cannot cast inherited class to base class
- What is the best way to clone/deep copy a .NET generic Dictionary?
- Generic constraints, where T : struct and where T : class
- Nullable type issue with ?: Conditional Operator
- How do I implement IEnumerable
- What does “where T : class, new()” mean?
- Nullable reference types with generic return type
- C# Generics won’t allow Delegate Type Constraints
- Generic C# Code and the Plus Operator [duplicate]
- Curiously Recurring Template Pattern and generics constraints (C#)
- Is there a generic constraint I could use for the + operator?
- Why doesn’t C# support implied generic types on class constructors?
- Why does this generic method require T to have a public, parameterless constructor?
- Are static members of a generic class tied to the specific instance?
- Generics with Generic Parameters and Abstract class
- How to constrain generic type to must have a construtor that takes certain parameters?
- How to write this EF Mock setup code as a reusable Generic Boilerplate?
- .NET Managing Layers Relationships