by Paulo Morgado via Paulo Morgado : C# on 4/14/2010 11:50:42 PM
In my last post, I went through what is variance in .NET 4.0 and C# 4.0 in a rather theoretical way.
Now, I’m going to try to make it a bit more down to earth.
Given:
class Base { } class Derived : Base { }
Such that:
Trace.Assert(typeof(Base).IsClass && typeof(Derived).IsClass && typeof(Base).IsGreaterOrEqualTo(typeof(Derived)));
interface ICovariantIn<out T> { }
Trace.Assert(typeof(ICovariantIn<Base>).IsGreaterOrEqualTo(typeof(ICovariantIn<Derived>)));
interface IContravariantIn<in T> { }
Trace.Assert(typeof(IContravariantIn<Derived>).IsGreaterOrEqualTo(typeof(IContravariantIn<Base>)));
interface IInvariantIn<T> { }
Trace.Assert(!typeof(IInvariantIn<Base>).IsGreaterOrEqualTo(typeof(IInvariantIn<Derived>)) && !typeof(IInvariantIn<Derived>).IsGreaterOrEqualTo(typeof(IInvariantIn<Base>)));
Where:
public static class TypeExtensions { public static bool IsGreaterOrEqualTo(this Type self, Type other) { return self.IsAssignableFrom(other); } }
Original Post: C# 4.0: Covariance And Contravariance In Generics Made Easy
The content of the postings is owned by the respective author. CSharpFeeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on CSharpFeeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.