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. validate.plugin.js 验证插件

    /*编写时间:2015-6-4*/ (function ($) { $.fn.isValidate = function (obj) { if ($(this).val()!="" ...

  2. C#主线程等待子线程运行结束

    佐左佑右 原文 C#主线程等待子线程运行结束 由于主程序中调用matlab的dll文件进行计算要用较长的时间,主界面会有很长时间的卡顿,造成的用户感受十分不好,因此我想在调用时,将调用放入子线程中,然 ...

  3. android模块化app开发-4为APP减负

    现在android应用中一个趋势是应用越来越大,免去游戏不谈普通APP也是一个个的体积直线增长.这里面除了业务增长外各种接口jar包的对接也占了不少比重.像广告SDK,统计SDK,支付SDK等这些我们 ...

  4. CAKeyframeAnimation

    之所以叫做关键帧动画是因为,这个类可以实现,某一属性按照一串的数值进行动画,就好像制作动画的时候一帧一帧的制作一样. 一般使用的时候  首先通过 animationWithKeyPath 方法 创建一 ...

  5. 试验Windows Embedded Standard 7 Service Pack 1 Evaluation Edition

    =========================================== 是否支持再使用 RT 7 Lite 精简 ? ================================= ...

  6. BootStrap入门教程 (三) :可重用组件(按钮,导航,标签,徽章,排版,缩略图,提醒,进度条,杂项)

    上讲回顾:Bootstrap的基础CSS(Base CSS)提供了优雅,一致的多种基础Html页面要素,包括排版,表格,表单,按钮等,能够满足前端工程师的基本要素需求. Bootstrap作为完整的前 ...

  7. select XXX into 和 Insert into XXX select

    检索一个表中的部分行存到另一张表中. 一 .另外的那张表没有新建的时候,使用 select XXX into,创建的表与原表有相同的列名和类型: select * into Departments_C ...

  8. webSocket vnc rfb

  9. hive常见问题解决干货大全

    本人,苦心多时,历经磨难和心血,与大家共同攻克问题难关! 问题一: FAILED: Execution Error, return code 1 from org.apache.hadoop.hive ...

  10. 设置结点的ID为固定ID

    https://www.java.net//forum/topic/jxta/jxta-community-forum/how-initialize-pse-jxse-27 ————————————— ...