Ray's playground

 

Creating Types in C#(Chapter 3 of C# 4.0 in a nutshell)

  Assuming S subclasses B, type X is covariant if X<S> allows a reference conversion to X<B>.As of C# 4.0, generic interfaces permit covariance (as do generic delegates), but generic classes do not. Arrays also support covariance (S[] can be cast to B[] if S subclasses B), and are discussed here for comparison.

  We previously saw that a type X is covariant if X<S> allows a reference conversion to X<B> where S subclasses B. A type is contravariant when you can convert in the reverse direction—from X<B> to X<S>. This is supported in C# 4.0 with generic interfaces when the generic type parameter only appears in input positions, designated with the in modifier. Extending our previous example, if the Stack<T> class implements the following interface:

  public interface IPushable<in T> { void Push (T obj); } 

posted on 2010-05-23 01:50  Ray Z  阅读(218)  评论(0编辑  收藏  举报

导航