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的更多相关文章

  1. 不变性、协变性和逆变性(Invariance, Covariance & Contravariance)

    源码下载 一.里氏替换原则(Liskov Substitution Principle LSP) 我们要讲的不是协变性和逆变性(Covariance & Contravariance)吗?是的 ...

  2. 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 ...

  3. 协变(covariance),逆变(contravariance)与不变(invariance)

    协变,逆变与不变 能在使用父类型的场景中改用子类型的被称为协变. 能在使用子类型的场景中改用父类型的被称为逆变. 不能做到以上两点的被称为不变. 以上的场景通常包括数组,继承和泛型. 协变逆变与泛型( ...

  4. Covariance and Contravariance (C#)

    Covariance and Contravariance (C#) https://docs.microsoft.com/en-us/dotnet/articles/csharp/programmi ...

  5. C# 逆变(Contravariance)/协变(Covariance) - 个人的理解

    逆变(Contravariance)/协变(Covariance) 1. 基本概念 官方: 协变和逆变都是术语,前者指能够使用比原始指定的派生类型的派生程度更大(更具体的)的类型,后者指能够使用比原始 ...

  6. 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 ...

  7. C#中的协变(Covariance)和逆变(Contravariance)

    摘要 ● 协变和逆变的定义是什么?给我们带来了什么便利?如何应用? ● 对于可变的泛型接口,为什么要区分成协变的和逆变的两种?只要一种不是更方便吗? ● 为什么还有不可变的泛型接口,为什么有的泛型接口 ...

  8. 《徐徐道来话Java》(2):泛型和数组,以及Java是如何实现泛型的

    数组和泛型容器有什么区别 要区分数组和泛型容器的功能,这里先要理解三个概念:协变性(covariance).逆变性(contravariance)和无关性(invariant). 若类A是类B的子类, ...

  9. .NET中的逆变协变

    MSDN上的说法: 协变和逆变都是术语,前者指能够使用比原始指定的派生类型的派生程度更小(不太具体的)的类型,后者指能够使用比原始指定的派生类型的派生程度更大(更具体的)的类型----------(注 ...

随机推荐

  1. JavaScript基础篇最全

    本章内容: 简介 定义 注释 引入文件 变量 运算符 算术运算符 比较运算符 逻辑运算符 数据类型 数字 字符串 布尔类型 数组 Math 语句 条件语句(if.switch) 循环语句(for.fo ...

  2. ylbtech-SubwayNav(地铁线路导航)-数据库设计

    ylbtech-DatabaseDesgin:ylbtech-SubwayNav(地铁线路导航)-数据库设计 DatabaseName:SubwayNav(地铁线路导航) Type:线路导航 1.A, ...

  3. 10、TV UI

     TV UI布局 1. 为大屏幕提供适当的布局源文件. 2. 确保UI在一定距离仍然可以看清. 3. 为高清电视提供高分辨率的图标和图像. 1. 把屏幕上的导航控制菜单放在屏幕的左边或者右边,并且将 ...

  4. T-SQL 常用语句学习

    一.基础 1.说明:创建数据库 CREATE DATABASE database-name 2.说明:删除数据库 drop database dbname 3.说明:备份sql server ---  ...

  5. linux常用命令之--文件打包与压缩命令

    linux的文件打包与压缩命令 1.压缩与解压命令 compress:用于压缩指定的文件,后缀为.z 其命令格式如下: compress [-d] 文件名 常用参数: -d:解压被压缩的文件(.z为后 ...

  6. QT多线程笔记

    1.QT多线程涉及到主线程和子线程之间交互大量数据的时候,使用QThread并不方便,因为run()函数本身不能接受任何参数,因此只能通过信号和槽的交互来获取数据,如果只是单方面简单交互数据还过得去, ...

  7. [Hive - LanguageManual] Statistics in Hive

    Statistics in Hive Statistics in Hive Motivation Scope Table and Partition Statistics Column Statist ...

  8. JavaEE5 Tutorial_Servlet

    Web资源:web组件,静态web文件如图片 Web程序:可发布的Web资源集合   Web程序根目录下有个web-inf文件夹,如果只有jsp和静态资源,里面可以没有web.xml 根目录下可以直接 ...

  9. Hadoop中FileSystem的append方法

    今天在使用Hadoop 1.1.2版本进行FileSystem的append操作时报以下异常: org.apache.hadoop.ipc.RemoteException: java.io.IOExc ...

  10. The Datastore

    [中央数据库模式难扩展]绝大多数的Web应用在处理一个为了以后的请求作检索用的请求时,需要存储信息.<1.Most useful web applications need to store i ...