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上的说法: 协变和逆变都是术语,前者指能够使用比原始指定的派生类型的派生程度更小(不太具体的)的类型,后者指能够使用比原始指定的派生类型的派生程度更大(更具体的)的类型----------(注 ...
随机推荐
- Java基础——异常处理
异常的层次结构 所有的异常类都是 java.lang.Exception 类的子类型.异常类都是 Throwable 类的子类.除了异常类 Error 类也是由 Throwable 类产生的的子类1. ...
- jquery加入购物车飞入的效果
主要原理是:点击当前图片的时候,复制(克隆)当前图片在当前位置,然后利用jQuery的animate()方法实现图像的飞入效果 效果预览:http://runjs.cn/detail/qmf0mtm1 ...
- Oracle的OracleBulkCopy不支持事务处理
在进行OracleBulkCopy批量数据导入的过程中使用事务后抛出了异常, 没使用事务时可以正确批量导入, ORA-12154:无法解析指定的连接字符串, 但是TNS配置肯定是没有错的, 难道是Co ...
- POJ 1094 Sorting It All Out
题意:给出m对关于n个字母的小于关系,输出通过这些关系能得到的结论,如果可以排序就输出至少知道第几个关系时就可以知道顺序,从小到大输出顺序:如果产生歧义就输出在第几个关系时出现歧义,如果不能得出准确的 ...
- 【Python】python读取文件操作mysql
尾大不掉,前阵子做检索测试时,总是因为需要业务端操作db和一些其他服务,这就使得检索测试对环境和数据依赖性特别高,极大提高了测试成本. Mock服务和mysql可以很好的解决这个问题,所以那阵子做了两 ...
- JDBC获取表的主键
JDBC获取表的主键 案例,创建订单,并根据订单号向订单明细表插入数据 sql语句: 创建两表 create table orders( id number(4) primary key, cus ...
- 添加删除ASM磁盘
创建磁盘: [root@kel ~]# oracleasm createdisk KEL3 /dev/sdf1 Writing disk header: done Instantiating disk ...
- Python中的__init__,__call__
__init__函数 当一个类实例被创建时, __init__() 方法会自动执行,在类实例创建完毕后执行,类似构建函数.__init__() 可以被当成构建函数,不过不象其它语言中的构建函数,它并不 ...
- Chapter4:表达式
左值和右值 当一个对象被用作右值的时候,用的是对象的值(内容),当对象被用作左值的时候,用的是对象的身份(在内存中的位置). 一个重要的原则是需要右值的地方可以用左值来代替,但是不能把右值当作左值使用 ...
- 在阿里云linux下使用SVN访问VisualSVN出错:SSL handshake failed: SSL error: Key usage violation in certificate has been detected
Subversion clients receive the following error message when attempting to connect to VisualSVN Serve ...