Covariance and Contra-variance

转载 2012年03月31日 13:38:51
Previous to .NET 4, generic interfaces were invariant. .NET 4 adds an important extension for generic
interfaces and generic delegates with covariance and contra-variance. Covariance and contra-variance
are about the conversion of types with argument and return types. For example, can you pass a
Rectangle to a method that requests a Shape? Let’s get into examples to see the advantages of these
With .NET, parameter types are covariant. Assume you have the classes Shape and Rectangle, and
Rectangle derives from the Shape base class. The Display() method is declared to accept an object of the
Shape type as its parameter:
public void Display(Shape o) { }
Now you can pass any object that derives from the Shape base class. Because Rectangle derives
from Shape, a Rectangle fulfills all the requirements of a Shape and the compiler accepts this

method call:

Rectangle r = new Rectangle { Width= 5, Height=2.5};


Return types of methods are contra - variant. When a method returns a Shape it is not possible to assign it to
a Rectangle because a Shape is not necessarily always a Rectangle . The opposite is possible. If a method
returns a Rectangle as the GetRectangle() method,
public Rectangle GetRectangle();
the result can be assigned to a Shape .
Shape s = GetRectangle();
Before version 4 of the .NET Framework, this behavior was not possible with generics. With C# 4, the
language is extended to support covariance and contra - variance with generic interfaces and generic
delegates. Let ’ s start by defi ning a Shape base class and a Rectangle class:

public class Shape
public double Width { get; set; }
public double Height { get; set; }
public override string ToString()
return String.Format("Width: {0}, Height: {1}", Width, Height);

public class Rectangle: Shape

Covariance with generic interfaces
A generic interface is covariant if the generic type is annotated with the out keyword. This also means that
type T is allowed only with return types. The interface IIndex is covariant with type T and returns this type
from a read - only indexer:
public interface IIndex < out T >
T this[int index] { get; }
int Count { get; }

The RectangleCollection.GetRectangles() method returns a RectangleCollection that implements
the IIndex<Rectangle> interface, so you can assign the return value to a variable rectangle of the
IIndex<Rectangle> type. Because the interface is covariant, it is also possible to assign the returned value
to a variable of IIndex<Shape>. Shape does not need anything more than a Rectangle has to offer. Using
the shapes variable, the indexer from the interface and the Count property are used within the for loop:
static void Main()
IIndex<Rectangle> rectangles = RectangleCollection.GetRectangles();
IIndex<Shape> shapes = rectangles;
for (int i = 0; i < shapes.Count; i++)

Contra-Variance with generic interfaces
A generic interface is contra-variant if the generic type is annotated with the in keyword. This way the
interface is only allowed to use generic type T as input to its methods:

public interface IDisplay<in T>
void Show(T item);

The ShapeDisplay class implements IDisplay<Shape> and uses a Shape object as an input parameter:
public class ShapeDisplay: IDisplay<Shape>
public void Show(Shape s)
Console.WriteLine("{0} Width: {1}, Height: {2}", s.GetType().Name,
s.Width, s.Height);

Creating a new instance of ShapeDisplay returns IDisplay<Shape>, which is assigned to the
shapeDisplay variable. Because IDisplay<T> is contra-variant, it is possible to assign the result to
IDisplay<Rectangle> where Rectangle derives from Shape. This time the methods of the interface only
define the generic type as input, and Rectangle fulfills all the requirements of a Shape:
static void Main()
IDisplay<Shape> shapeDisplay = new ShapeDisplay();
IDisplay<Rectangle> rectangleDisplay = shapeDisplay;

Mean Vector and Covariance Matrix

点击打开链接 Mean Vector and Covariance Matrix The fir...
  • weixin_40844070
  • weixin_40844070
  • 2017年11月30日 20:10
  • 30


  • shen19920619
  • shen19920619
  • 2017年03月02日 11:25
  • 156

the summary of sklearn.covariance

sklearn.covariance has three categories:EmpiricalCovariance and so on,Shrunkage,GraphLasso. Empiric...
  • muzhen_xupeng
  • muzhen_xupeng
  • 2017年01月03日 10:00
  • 96

对协变和逆变的理解(Contravariance and Covariance)

本篇博客转载自。 在学习OpenCV过程中遇到了一个错误VS提示了这样一个错误: IntelliSen...
  • ZJ547877350
  • ZJ547877350
  • 2016年11月06日 01:11
  • 438

相干矩阵(coherency matrix)、协方差矩阵(covariance matrix)、散射矩阵

(1)Sinclair矩阵:通常情况下,雷达目标在远场区的电磁散射特性是一个线 性过程,如果选定了散射空间坐标系以及相应的极化基,那么雷达照射波和目标散射波的各极化分量之间存在着线性变换关系,因此...
  • zkl99999
  • zkl99999
  • 2015年04月13日 21:54
  • 3569

Variance-Covariance Matrix

This lesson explains how to use matrix methods to generate a variance-covariance matrix from a matri...
  • wusecaiyun
  • wusecaiyun
  • 2016年05月02日 22:04
  • 299


在概率论中,两个随机变量 X 与 Y 之间相互关系,大致有下列3种情况: 当 X, Y 的联合分布像上图那样时,我们可以看出,大致上有: X 越大  Y 也越大, X 越小 ...
  • xiewenbo
  • xiewenbo
  • 2013年11月21日 16:45
  • 607


In probability theory and statistics, covariance is a measure of how much two variables change tog...
  • 2011年09月29日 11:24
  • 368

协方差矩阵(covariance matrix)

Xn×d⇒(XTX)d×d X_{n\times d}\Rightarrow \left (X^TX\right )_{d\times d}(1)协方差矩阵:半正定(semi-positive def...
  • lanchunhui
  • lanchunhui
  • 2016年03月14日 20:01
  • 675

Region Covariance: A Fast Descriptor for Detection and Classification算法总结

  • u013089961
  • u013089961
  • 2015年11月16日 20:53
  • 776
您举报文章:Covariance and Contra-variance