在.net4之前,泛型接口是不变的。.net4通过协变和抗变为泛型接口和泛型委托添加了一个重要的拓展
1、抗变:如果泛型类型用out关键字标注,泛型接口就是协变的。这也意味着返回类型只能是T。
实例:
static void Main(string[] args)
{
IIndex rectangles = RectangleCollection.GetRectangles();
IIndex shapes = rectangles;
Console.ReadKey();
}
public interface IIndex
{
T this[int index] { get; }
int Count { get; }
}
public class RectangleCollection : IIndex
{
private Rectangle[] data = new Rectangle[]
{
new Rectangle{Width=,Height=},
new Rectangle{Width=,Height=},
new Rectangle{Width=4.5,Height=2.9}
};
private static RectangleCollection coll;
public static RectangleCollection GetRectangles()
{
return coll ?? (coll = new RectangleCollection());
}
public Rectangle this[int index]
{
get
{
if (index < || index > data.Length)
{
throw new ArgumentOutOfRangeException("index");
}
return data[index];
}
}
public int Count
{
get
{
return data.Length;
}
}
}
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
{
}
2、抗变:如果泛型类型用in关键字,泛型接口就是抗变得。这样,接口的只能把泛型类型T用作方法的输入
实例:
static void Main(string[] args)
{
IIndex rectangles = RectangleCollection.GetRectangles();
IDisplay shapeDisplay = new ShapeDisplay();
IDisplay rectangleDisplay = shapeDisplay;
rectangleDisplay.Show(rectangles[]);
Console.ReadKey();
}
public interface IDisplay
{
void Show(T item);
}
public class ShapeDisplay : IDisplay
{
public void Show(Shape item)
{
Console.WriteLine("{0} width:{1},height:{2}", item.GetType().Name, item.Width, item.Height);
}
}
C#泛型的抗变与协变
C#泛型的抗变与协变 学习自 C#本质论6.0 https://www.cnblogs.com/pugang/archive/2011/11/09/2242380.html Overview 一直以来 ...
Java泛型中的协变和逆变
Java泛型中的协变和逆变 一般我们看Java泛型好像是不支持协变或逆变的,比如前面提到的List和List之间是不可变的.但当我们在Java泛 ...
C#4.0新增功能03 泛型中的协变和逆变
连载目录 [已更新最新开发文章,点击查看详细] 协变和逆变都是术语,前者指能够使用比原始指定的派生类型的派生程度更大(更具体的)的类型,后者指能够使用比原始指定的派生类型的派生程度更小(不太具体 ...
.NET泛型03,泛型类型的转换,协变和逆变
协变(Convariant)和逆变(Contravariant)的出现,使数组.委托.泛型类型的隐式转换变得可能. 子类转换成基类,称之为协变:基类转换成子类,称之为逆变..NET4.0以来,支持了泛 ...
泛型中? super T和? extends T的区别
原文出处: 并发编程网 经常发现有List super T>.Set extends T>的声明,是什么意思呢? super T>表示包括T在内的任何T ...
Scala 深入浅出实战经典 第49课 Scala中Variance代码实战(协变)
王家林亲授
大数据实战视频 Scala 深入浅出实战经典(1-64讲)完整视频.PPT.代码下载:百度云盘:http://pan.baidu.com/s/1c0noOt6 ...Java泛型中E、T、K、V等的含义
Java泛型中的标记符含义: E - Element (在集合中使用,因为集合中存放的是元素) T - Type(Java 类) K - Key(键) V - Value(值) N - Numbe ...
java 泛型中 T、E ... 和 问号(通配符)的区别
一.泛型中T.E ... 是泛型类.泛型方法定义时候用的. 1.泛型类定义在类后面 紧跟类名后面 public class TestClassDefine{} 2.泛型方法定义在方 ...
Java泛型中extends和super的理解(转)
E – Element (在集合中使用,因为集合中存放的是元素) T – Type(Java 类) K – Key(键) V – Value(值) N – Number(数值类型) ? – 表示不确定 ...
随机推荐
C#——传值参数(1)
//我的C#是跟着猛哥(刘铁猛)(算是我的正式老师)学习的,微信上猛哥也给我讲解了一些不懂得地方,对于我来说简直是一笔巨额财富,难得良师! 这次与大家一起学习C#中的值 ...
activity跳转到新的activity后清除之前的activity
Intent intent = new Intent(A.this, B.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Inten ...
wpf 后台绘制圆弧
wpf 前台绘制圆弧很简单,如:
3xx Redirection
3xx Redirection This class of status code indicates the client must take additional action to comple ...
简单的java高斯模糊算法
import java.awt.Color; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOEx ...
linux 安装node.js 和npm
cd /usr/local mkdir nodejs cd nodejs 去https://nodejs.org/en/download/复制安装包地址 wget https://nodejs.org ...
React Native 调用 Web3(1.x) 的正确姿势
1 创建项目 react-native init lm1 cd lm1 2 安装依赖包 yarn add node-libs-browser 3 创建 rn-cli.config.js 脚本 cons ...
ZYNQ学习之路1. Linux最小系统构建
https://blog.csdn.net/u010580016/article/details/80430138?utm_source=blogxgwz1 开发环境:window10, vivado ...
Java并发集合(二)-ConcurrentSkipListMap分析和使用
一.ConcurrentSkipListMap介绍 ConcurrentSkipListMap是线程安全的有序的哈希表,适用于高并发的场景.ConcurrentSkipListMap和TreeMap, ...
(转)每天一个linux命令(21):find命令之xargs
原文:http://www.cnblogs.com/peida/archive/2012/11/15/2770888.html https://blog.csdn.net/ly1358152944/a ...