Covariance and Contravariance in C#, Part One

http://blogs.msdn.com/b/ericlippert/archive/2007/10/16/covariance-and-contravariance-in-c-part-one.aspx

I have been wanting for a long time to do a series of articles about covariance and contravariance (which I will shorten to “variance” for the rest of this series.)

I’ll start by defining some terms, then describe what variance features C# 2.0 and 3.0 already support today, and then discuss some ideas we are thinking about for hypothetical nonexistant future versions of C#.

As always, keep in mind that we have not even shipped C# 3.0 yet. Any of my musings on possible future additions to the language should be treated as playful hypotheses, rather than announcements of a commitment to ship any product with any feature whatsoever.

Today: what do we mean by “covariance” and “contravariance”?

The first thing to understand is that for any two types T and U, exactly one of the following statements is true:

  • T is bigger than U.
  • T is smaller than U.
  • T is equal to U.
  • T is not related to U.

For example, consider a type hierarchy consisting of Animal, Mammal, Reptile, Giraffe, Tiger, Snake and Turtle, with the obvious relationships. ( Mammal is a subclass of Animal, etc.) Mammal is a bigger type than Giraffe and smaller thanAnimal, and obviously equal to Mammal. But Mammal is neither bigger than, smaller than, nor equal to Reptile, it’s just different.

Why is this relevant? Suppose you have a variable, that is, a storage location. Storage locations in C# all have a type associated with them. At runtime you can store an object which is an instance of an equal or smaller type in that storage location. That is, a variable of type Mammal can have an instance of Giraffe stored in it, but not a Turtle.

This idea of storing an object in a typed location is a specific example of a more general principle called the “substitution principle”. That is, in many contexts you can often substitute an instance of a “smaller” type for a “larger” type.

Now we can talk about variance. Consider an “operation” which manipulates types. If the results of the operation applied to any T and U always results in two types T’ and U’ with the same relationship as T and U, then the operation is said to be “covariant”. If the operation reverses bigness and smallness on its results but keeps equality and unrelatedness the same then the operation is said to be “contravariant”.

That’s totally highfalutin and probably not very clear. Next time we’ll look at how C# 3 implements variance at present.

时间: 2024-08-25 14:00:38

Covariance and Contravariance in C#, Part One的相关文章

Covariance and Contravariance in C#, Part Two: Array Covariance

http://blogs.msdn.com/b/ericlippert/archive/2007/10/17/covariance-and-contravariance-in-c-part-two-array-covariance.aspx C# implements variance in two ways. Today, the broken way. Ever since C# 1.0, arrays where the element type is a reference type a

面向对象设计——协变与逆变

在面向对象的设计中,我们一直追求一种结果,就是良好的复用性,基于这个理念,面向对象的设计中加入了协变与逆变(Covariance and Contravariance)两个概念,我们先来简单了解一下这两个概念. 简介: 协变:由子类向父类方向转变, 用out关键字标识 逆变:由父类向子类方向转变, 用in关键字 举例:Animal是父类,Dog是从Animal继承的子类:如果一个对象的类型是Dog,那么他必然是Animal.有一个获取宠物的方法要接受Dog参数,那么另一个接受Animal参数的方

厘清泛型参数的协变与逆变

协变与逆变(CoVariant and ContraVariant),很多人是糊涂的,我也一直糊涂.其实,对协变与逆变概念糊涂,甚至完全不知道,对一般程序员也没有很大影响.不过,如果你想提高水平,想大概看懂.Net Framework类库中那些泛型接口与泛型类,想大概弄清楚Linq,这个概念还是需要搞清楚. 话又说回来,想弄清楚,其实还是挺费劲的. 如果你还糊涂着这两个概念,相信我,认真看完下面的文字,你会对泛型参数的协变与逆变有一个清晰的理解. 想透彻掌握协变.逆变的概念,首先需要对接口.委托

c#4.0新特性之协变与逆变

1.C#3.0以前的协变与逆变 如果你是第一次听说这个两个词,别担心,他们其实很常见.C#4.0中的协变与逆变[1](Covariance and contravariance)有了进一步的完善,主要是两种运行时的(隐式)泛型类型参数转换.简单来讲,所谓协变(Covariance)是指把类型从“小”升到“大”,比如从子类升级到父类:逆变则是指从“大”变到“小”,两者各有不同的条件和用途.下面的例子演示了C#3.0以前对协变与逆变支持[2] : 代码1        public class An

C#各版本新特性

转 C# 2.0 泛型(Generics) 泛型是CLR 2.0中引入的最重要的新特性,使得可以在类.方法中对使用的类型进行参数化. 例如,这里定义了一个泛型类: class MyCollection<T> { T variable1; private void Add(T param){ } } 使用的时候:MyCollection<string> list2 = new MyCollection<string>(); MyCollection<Object&g

Co-variant array conversion from x to y may cause run-time exception

http://stackoverflow.com/questions/8704332/co-variant-array-conversion-from-x-to-y-may-cause-run-time-exception What it means is this Control[] controls = new LinkLabel[10]; // compile time legal controls[0] = new TextBox(); // compile time legal, ru

.NET和C#版本历史概览

发布日期 .Net版本 C#版本 CLR版本 开发工具 功能介绍 2002 1.0 1.0 初始版本 Visual Studio .Net 初始版本 .NET框架结构,详见: 2003 1.1     Visual Studio 2003 ASP.NET 移动控件 ADO.NET增强(ODBC.Oracle.分布式事务) 多版本共存(Side-by-Side Execution) Etc. see: http://msdn.microsoft.com/en-us/library/h88tthh0

Nemerle Quick Guide

This is a quick guide covering nearly all of Nemerle's features. It should be especially useful to anyone who is already familiar with C# or a similar language: Table of Contents Variables Operators Logical Operators Bit Operators Type Casts/Conversi

C#各版本新特性(转)

C# 2.0 泛型(Generics) 泛型是CLR 2.0中引入的最重要的新特性,使得可以在类.方法中对使用的类型进行参数化. 例如,这里定义了一个泛型类: class MyCollection<T> { T variable1; private void Add(T param){ } } 使用的时候:MyCollection<string> list2 = new MyCollection<string>(); MyCollection<Object>