ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-SimpleVariance
1.A,示例(Sample) 返回顶部

SimpleVariance 示例程序演示在委托和接口中使用泛型类型时,C# 4.0 对协变和逆变的支持。 在 C# 3.0 中,泛型类型是固定的。 因此,本示例在 C# 4.0 中可正常编译,但在早期版本的 C# 中无法编译。

程序首先声明两个简单类和两个委托:

class  Animal { }
class Cat : Animal { } delegate T Func1<out T>();
delegate void Action1<in T>(T a);

然后,实现这些委托并使用这些类:

Func1<Cat> cat = () => new Cat();
Action1<Animal> act1 = (ani) => { Console.WriteLine(ani); };

再进行需要协变和逆变支持的分配:

Func1<Animal> animal = cat;
Action1<Cat> cat1 = act1;

后面这一组分配在 C# 4.0 中可以正常工作,但在早期版本的 C# 中无法工作。 第一个分配演示协变,第二个分配演示逆变。

通过新增的上下文关键字 outin,可以指定泛型类型是要传递到委托或接口方法中,还是要从委托或接口方法返回。

1.B,SimpleVariance 示例代码(Sample Code)返回顶部

1.B.1, Program.cs

// 版权所有(C) Microsoft Corporation。保留所有权利。
// 此代码的发布遵从
// Microsoft 公共许可(MS-PL,http://opensource.org/licenses/ms-pl.html)的条款。
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace SimpleVariance
{
class Animal { }
class Cat: Animal { } class Program
{
// 要了解新的协变和逆变代码能够为您实现的功能
// 尝试从以下两行代码中添加或删除这些内容:
delegate T Func1<out T>();
delegate void Action1<in T>(T a); static void Main(string[] args)
{
Func1<Cat> cat = () => new Cat();
Func1<Animal> animal = cat; Action1<Animal> act1 = (ani) => { Console.WriteLine(ani); };
Action1<Cat> cat1 = act1; Console.WriteLine(animal());
cat1(new Cat());
} }
}

1.B.2,

1.B.EXE,

SimpleVariance.Cat
SimpleVariance.Cat
请按任意键继续. . .

1.B,

1.C,下载地址(Free Download)返回顶部
作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

ylbtech-LanguageSamples-SimpleVariance的更多相关文章

  1. Java-Class-Miniprogram:com.ylbtech.common.utils.miniprogram.TemplateMessage

    ylbtech-Java-Class-Miniprogram:com.ylbtech.common.utils.miniprogram.TemplateMessage 1.返回顶部 1.1. pack ...

  2. Java-Class-C:com.ylbtech.api.platfrom.util.RedisUtils.class

    ylbtech-Java-Class-C:com.ylbtech.api.platfrom.util.RedisUtils.class 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶 ...

  3. ylbtech-Miscellaneos

    ylbtech-Miscellaneos: A,返回顶部 1, 2, B返回顶部 1, 2 作者:ylbtech出处:http://ylbtech.cnblogs.com/本文版权归作者和博客园共有, ...

  4. Azure

    ylbtech-Miscellaneos:Azure A,返回顶部 1, Windows Azure是微软基于云计算的操作系统,现在更名为“Microsoft Azure”,和Azure Servic ...

  5. GUI(图形用户界面)

    ylbtech-Miscellaneos:GUI(图形用户界面) A,返回顶部 1, 图形用户界面(Graphical User Interface,简称 GUI,又称图形用户接口)是指采用图形方式显 ...

  6. SOAP(简单对象访问协议)

    ylbtech-Miscellaneos:SOAP(简单对象访问协议) A,返回顶部 1, 简单对象访问协议是交换数据的一种协议规范,是一种轻量的.简单的.基于XML(标准通用标记语言下的一个子集)的 ...

  7. WS-Security

    ylbtech-Miscellaneos: WS-Security A,返回顶部 1, WS-Security (Web服务安全) 是一种提供在Web服务上应用安全的方法的网络传输协议. 2004年4 ...

  8. 企业架构(Enterprise Architecture)

    ylbtech-Miscellaneos: 企业架构(Enterprise Architecture) A,返回顶部 1, 简称EA.是指对企业事业信息管理系统中具有体系的.普遍性的问题而提供的通用解 ...

  9. ASP.NET中 WebForm 窗体控件使用及总结【转】

    原文链接:http://www.cnblogs.com/ylbtech/archive/2013/03/06/2944675.html ASP.NET中 WebForm 窗体控件使用及总结. 1.A, ...

随机推荐

  1. CodeForces 450B

    B. Jzzhu and Sequences time limit per test 1 second memory limit per test 256 megabytes input standa ...

  2. ZOJ-3430

    Detect the Virus  Time Limit: 2 Seconds      Memory Limit: 65536 KB One day, Nobita found that his c ...

  3. JS框架的实现

    众多流行的JS库都不同程度的污染了原生JS,最典型的如Prototype ,Mootools .JQuery则完全例外,一个匿名函数执行后便诞生了集所有API为一身的强大 $ .虽然如此,JQuery ...

  4. Partial Views

    @Html.Partial("MyPartial")   @Html.Partial("MyStronglyTypedPartial", new [] {&qu ...

  5. 【剑指offer】面试题 9. 用两个栈实现队列

    面试题 9. 用两个栈实现队列 题目描述 题目:用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 解答过程 import java.util.Stack; publ ...

  6. 大数据技术之_16_Scala学习_05_面向对象编程-中级

    第七章 面向对象编程-中级7.1 包7.1.1 Java 中的包7.1.2 Scala 中的包7.1.3 Scala 包的特点概述7.1.4 Scala 包的命名7.1.5 Scala 会自动引入的常 ...

  7. 洛谷——P2908 [USACO08OPEN]文字的力量Word Power

    P2908 [USACO08OPEN]文字的力量Word Power 题目描述 Farmer John wants to evaluate the quality of the names of hi ...

  8. [BZOJ 1724] Fence Repair

    这大概是BZOJ里除了A+B Problem最水的一道题了吧 题面:http://www.lydsy.com/JudgeOnline/problem.php?id=1724 这道题其实有一些思路还是可 ...

  9. poj 1348 Period(KMP)

    Period Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  10. BZOJ 3022 [Balkan2012]The Best Teams(扫描线+线段树)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3022 [题目大意] 给定n个球员,第i个球员年龄为AGEi,水平为SKILLi. 没有 ...