Covarience And ContraVariance
using System;
using System.Collections.Generic;
using System.IO; namespace CovarientAndContraVarient
{
class Program
{
static object GetObject() { return null; }
static void SetObject(object obj) { } static string GetString() { return ""; }
static void SetString(string str) { } static void Main(string[] args)
{
Func<object> getString = GetString; Action<string> setString = SetObject; Func<string> getString1 = GetString; //1. Delegate variable implicitly cast is not valid until .net 4.0
Func<object> setString2 = getString1; //Assignment compatibility,
IEnumerable<String> test = new List<string>(); //covariance OUT == > Covariance
IEnumerable<object> test1 = new List<string>(); //Assignment compatibility
Action<Object> objAction = StaticMethod; //ContraConvariance , IN === > ContraConvariance
Action<String> stringAction = StaticMethod; //2. Array Covariance, support in C# 1.0
object[] objArray = new String[] { "a", "b", "c" }; //Covariance in array is not safe, below code will throw exception
objArray[] = ; //3. Co/contra- Variance don't support value type, below code is invalid
//IEnumerable<object> objects = new List<int>(); //you can use an interface instance that has methods with more
//derived return types than originally specified (covariance--OUT)
//or that has methods with less derived parameter types (contravariance--IN). //Contravariance
IMyTest<FileStream> myTestInstance = new MyTestClass1<Stream>(); IMyTest<Stream> myTest = new MyTestClass1<Stream>(); //covariance
IMyTest1<object> myTest1Instance = new MyTestClass2<string>(); IMyTest<FileStream> myTestInstance1 = myTest; //Below Code, you will think it is a little strange, but absolutely it works well!!!!!!!!!
//4. You can mark a generic type parameter as covariant if it is used only as a method return
//type and is not used as a type of formal method parameters.
//5. And vice versa, you can mark a type as contravariant if it is used only as a type of
//formal method parameters and not used as a method return type.
IMyFirstTestClass<object, string> testClass = null;
IMyFirstTestClass<string, object> testClass1 = testClass;
} public static void StaticMethod(object o)
{
}
} public interface IMyFirstTestClass<in T1, out T2>
{
T2 DoSomething(T1 para);
} //6. Variant type only can declared in interfaces and delegates only!!!!!!!
//public class MyTestClass<in T>
//{ //} //Contravariance
public interface IMyTest<in T>
{
void PrintTest(T param);
} //7. Below code, T is invariance, if you want to it be Contravariance, you must declare it explicitly.
//Same as covariance
public interface IMyTestExtend<T> : IMyTest<T>
{
} public class MyTestClass1<T> : IMyTest<T>
{
public void PrintTest(T para)
{
Console.WriteLine("This is " + typeof(T) + " PrintTest Method!");
}
} //Covariance
public interface IMyTest1<out T>
{
T PrintTest();
} public class MyTestClass2<T> : IMyTest1<T>
{
public T PrintTest()
{
Console.WriteLine("This is " + typeof(T) + " PrintTest Method!");
return default(T);
}
} public delegate void MyHander<in T>(T para); //Below method declaration is invalid, because 'in' is contravariance, it only can be in paramter type
//public delegate T MyHander1<in T>(T para); public delegate T MyHandler2<out T>(object para); //Below method declaration is invalid, because 'out' is covariance, it only can be in returned type
//public delegate void MyHandler2<out T>(T para); }
FYI: http://blogs.msdn.com/b/csharpfaq/archive/2010/02/16/covariance-and-contravariance-faq.aspx
Covarience And ContraVariance的更多相关文章
- 不变性、协变性和逆变性(Invariance, Covariance & Contravariance)
源码下载 一.里氏替换原则(Liskov Substitution Principle LSP) 我们要讲的不是协变性和逆变性(Covariance & Contravariance)吗?是的 ...
- Covariance and Contravariance in C#, Part One
http://blogs.msdn.com/b/ericlippert/archive/2007/10/16/covariance-and-contravariance-in-c-part-one.a ...
- 协变(covariance),逆变(contravariance)与不变(invariance)
协变,逆变与不变 能在使用父类型的场景中改用子类型的被称为协变. 能在使用子类型的场景中改用父类型的被称为逆变. 不能做到以上两点的被称为不变. 以上的场景通常包括数组,继承和泛型. 协变逆变与泛型( ...
- Covariance and Contravariance (C#)
Covariance and Contravariance (C#) https://docs.microsoft.com/en-us/dotnet/articles/csharp/programmi ...
- C# 逆变(Contravariance)/协变(Covariance) - 个人的理解
逆变(Contravariance)/协变(Covariance) 1. 基本概念 官方: 协变和逆变都是术语,前者指能够使用比原始指定的派生类型的派生程度更大(更具体的)的类型,后者指能够使用比原始 ...
- 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-a ...
- C#中的协变(Covariance)和逆变(Contravariance)
摘要 ● 协变和逆变的定义是什么?给我们带来了什么便利?如何应用? ● 对于可变的泛型接口,为什么要区分成协变的和逆变的两种?只要一种不是更方便吗? ● 为什么还有不可变的泛型接口,为什么有的泛型接口 ...
- 《徐徐道来话Java》(2):泛型和数组,以及Java是如何实现泛型的
数组和泛型容器有什么区别 要区分数组和泛型容器的功能,这里先要理解三个概念:协变性(covariance).逆变性(contravariance)和无关性(invariant). 若类A是类B的子类, ...
- .NET中的逆变协变
MSDN上的说法: 协变和逆变都是术语,前者指能够使用比原始指定的派生类型的派生程度更小(不太具体的)的类型,后者指能够使用比原始指定的派生类型的派生程度更大(更具体的)的类型----------(注 ...
随机推荐
- VS2010下编译安装DarwinStreamingServer5.5.5
源码下载链接:http://dss.macosforge.org/源码版本: 5.5.5版本电脑环境:visual studio2010,window 7 x64系统.用VS2010打开WinNTSu ...
- uva 11768
// 扩展欧几里得算法 // 先求出一个解 再求出区间 [x1,x2]有几个整数符合条件// 需要注意的是 水平和垂直2种情况的处理 还有正数和负数取整的细微差别#include <iostre ...
- Js原型模式
function Person(){ } Person.prototype.name = "xd"; Person.prototype.age = 26; Person.proto ...
- [Everyday Mathematics]20150108
设 $f$ 在 $(a,b)$ 上 $n+1$ 次可导, 且 $$\bex \ln\frac{f(b)+f'(b)+\cdots+f^{(n)}(b)}{f(a)+f'(a)+\cdots+f^{(n ...
- FastDFS总结
前言 FastDFS主要解决互联网中小文件存储存储问题,例如图片,短视频,提供上传和下载功能,轻量级的设计,结构非常简单,主要包含三个角色客户端,Tracer服务,Storage服务.Tracer服务 ...
- [转]Oracle 阳历转农历
CREATE TABLE SolarData ( YearID INTEGER NOT NULL, -- 农历年 DATA ) NOT NULL, -- 农历年对应的16进制数 DataInt INT ...
- 使用std::function 把类成员函数指针转换为普通函数指针
前言 这是改造前一篇 设计模式 的基础,使通知者不必知道观察者的类名和函数名,只需要知道更新函数的原型即可. 开发环境:WIN7 32位 + VS2010 发现在VS2005中使用std::funti ...
- motan解读:添加spring 支持
代码位置: motan-core的目录下 motan中使用spring管理配置对象.motan利用Spring的spi机制创建了自定义标签和相应的标签处理代码.具体使用方法见这篇.本文以m ...
- 杂谈:HTML 5页面可视性API
译文来源:http://www.ido321.com/1126.html 原文:HTML5 Page Visibility API 译文:HTML 5的页面可视性API 译者:dwqs 在早期,浏览器 ...
- PHP的MySQL扩展:PHP访问MySQL的常用扩展函数
来源:http://www.ido321.com/1024.html 一.PHP连接数据库及基本操作 MySQL采用的是’客户机/服务器’架构.使用PHP安装的MySQL扩展函数,和直接使用客户端软件 ...