C#中的协变OUT和逆变
泛型接口和泛型委托中经常使用可变性
in 逆变,out 协变
从 list<string>转到list<object> 称为协变 (string 从object 派生,那么 string 转成object 是合理的,子类替换父类是合理的,)
从list<object> 转到 list<string> 称为逆变 (经object 转成string ,将父类转成子类,是不合理的,称为逆变)
1.逆变代码掩饰
static void Main(string[] args)
{
ICovariant<Object> iobj = new Sample<Object>();
ICovariant<String> istr = new Sample<String>(); // You can assign istr to iobj because
// the ICovariant interface is covariant.
// iobj = istr;
istr = iobj; //从 object 类型转成string 类型,父类转成子类 则是逆变
Console.ReadLine();
} // Covariant interface.
interface ICovariant<in R> { } // Extending covariant interface.
interface IExtCovariant<in R> : ICovariant<R> { } // Implementing covariant interface.
class Sample<R> : ICovariant<R> { }
2.协变演示
static void Main(string[] args)
{
ICovariant<Object> iobj = new Sample<Object>();
ICovariant<String> istr = new Sample<String>(); // You can assign istr to iobj because
// the ICovariant interface is covariant.
// iobj = istr;
iobj = istr; //从 string类型转成object类型,子类型转成父类 (里氏替换原则) 则是协变
Console.ReadLine();
} // Covariant interface.
interface ICovariant<out R> { } // Extending covariant interface.
interface IExtCovariant<out R> : ICovariant<R> { } // Implementing covariant interface.
class Sample<R> : ICovariant<R> { }
C#中的协变OUT和逆变的更多相关文章
- C#中的协变(Covariance)和逆变(Contravariance)
摘要 ● 协变和逆变的定义是什么?给我们带来了什么便利?如何应用? ● 对于可变的泛型接口,为什么要区分成协变的和逆变的两种?只要一种不是更方便吗? ● 为什么还有不可变的泛型接口,为什么有的泛型接口 ...
- 协变(covariant)和逆变(contravariant)
我们知道子类转换到父类,在C#中是能够隐式转换的.这种子类到父类的转换就是协变. 而另外一种类似于父类转向子类的变换,可以简单的理解为“逆变”. 上面对逆变的简单理解有些牵强,因为协变和逆变只能针对接 ...
- C# 协变out 、逆变 in
需求:泛型使用多态性 备注:协变逆变只能修饰 接口和委托 简单理解: 1.使用 in 修饰后为逆变,只能用作形参使用 ,参考 public delegate void Action<in T&g ...
- Java中的逆变与协变
看下面一段代码 Number num = new Integer(1); ArrayList<Number> list = new ArrayList<Integer>(); ...
- .NET 4.0中的泛型的协变和逆变
转自:http://www.cnblogs.com/jingzhongliumei/archive/2012/07/02/2573149.html 先做点准备工作,定义两个类:Animal类和其子类D ...
- 转载.NET 4.0中的泛型的协变和逆变
先做点准备工作,定义两个类:Animal类和其子类Dog类,一个泛型接口IMyInterface<T>, 他们的定义如下: public class Animal { } public ...
- Java中的逆变与协变(转)
看下面一段代码 Number num = new Integer(1); ArrayList<Number> list = new ArrayList<Integer>(); ...
- Scala中的协变,逆变,上界,下界等
Scala中的协变,逆变,上界,下界等 目录 [−] Java中的协变和逆变 Scala的协变 Scala的逆变 下界lower bounds 上界upper bounds 综合协变,逆变,上界,下界 ...
- C#中泛型方法与泛型接口 C#泛型接口 List<IAll> arssr = new List<IAll>(); interface IPerson<T> c# List<接口>小技巧 泛型接口协变逆变的几个问题
http://blog.csdn.net/aladdinty/article/details/3486532 using System; using System.Collections.Generi ...
随机推荐
- PAT (Advanced Level) 1110. Complete Binary Tree (25)
判断一棵二叉树是否完全二叉树. #include<cstdio> #include<cstring> #include<cmath> #include<vec ...
- 理解free命令
free的输出: total used free shared buffers cached Mem: -/+ buffers/cache: Swap: 第一行: total:总内存 used:已使用 ...
- 【项目笔记】拿宽高前measure(widthMeasureSpec, heightMeasureSpec)的使用技巧
我们知道获取宽高一般写法是: view.measure(0, 0); view.getMeasuredHeight(); 拿宽高前什么时候可以直接用measure(0, 0);而什么时候不能用meas ...
- BD string 百度之星初赛的题目 数学
http://acm.hdu.edu.cn/showproblem.php?pid=5694 可以说这道题目挺考验思维含量的吧 首先,这个和数位dp是差不多的方法,计算cal(l)-cal(l-1): ...
- html-关于IE浏览器兼容性的问题,还有浏览器一直加载的问题。
今天9月11日,所谓兼容性问题就是,标签 写的不规范导致的,因为高版本的IE会自动补上没写的标签 document.write('<object classid="clsid:d27c ...
- struts1.x中web.xml文件的配置
1.配置欢迎文件清单 当客户访问Web应用时,如果仅仅给出Web应用的Root URL,没有指定具体的文件名.Web容器会自动调用Web应用的欢迎文件.<welcome-file-li ...
- JS读RSS
<html> <head> <title>javascript读取RSS数据</title> <META content="t ...
- http 安全验证
今天升级Xcode 7.0 bata发现网络访问失败.输出错误信息 The resource could not be loaded because the App Transport Securit ...
- LINQ to SQL语句对应SQL的实现
LINQ to SQL语句(1)之Where LINQ to SQL语句(2)之Select/Distinct LINQ to SQL语句(3)之Count/Sum/Min/Max/Avg LINQ ...
- Object.defineproperty实现数据和视图的联动 ------是不是就是 Angular 模型和视图的同步的实现方式???
参考:http://www.cnblogs.com/oceanxing/p/3938443.html https://developer.mozilla.org/zh-CN/docs/Web/Java ...