Ray's playground

 

Item 29: Support Generic Covariance and Contravariance(Effective C#)

It certainly can get complicated describing exactly how covariance and contravariance work. Thankfully, now the language supports decorating generic interfaces and delegates with in (contravariant) and out (covariant) modifiers. You should decorate any interfaces and delegates you define with the in or out modifiers wherever possible. Then, the compiler can correct any possible misuses of the variance you’ve defined. The compiler will catch it both in your interface and delegate definitions, and it will detect any misuse of the types you’ve created.

posted on 2011-02-19 11:12  Ray Z  阅读(142)  评论(0编辑  收藏  举报

导航