ylbtech-LanguageSamples-SimpleVariance
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# 中无法工作。 第一个分配演示协变,第二个分配演示逆变。
通过新增的上下文关键字 out 和 in,可以指定泛型类型是要传递到委托或接口方法中,还是要从委托或接口方法返回。
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的更多相关文章
- Java-Class-Miniprogram:com.ylbtech.common.utils.miniprogram.TemplateMessage
ylbtech-Java-Class-Miniprogram:com.ylbtech.common.utils.miniprogram.TemplateMessage 1.返回顶部 1.1. pack ...
- 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.返回顶 ...
- ylbtech-Miscellaneos
ylbtech-Miscellaneos: A,返回顶部 1, 2, B返回顶部 1, 2 作者:ylbtech出处:http://ylbtech.cnblogs.com/本文版权归作者和博客园共有, ...
- Azure
ylbtech-Miscellaneos:Azure A,返回顶部 1, Windows Azure是微软基于云计算的操作系统,现在更名为“Microsoft Azure”,和Azure Servic ...
- GUI(图形用户界面)
ylbtech-Miscellaneos:GUI(图形用户界面) A,返回顶部 1, 图形用户界面(Graphical User Interface,简称 GUI,又称图形用户接口)是指采用图形方式显 ...
- SOAP(简单对象访问协议)
ylbtech-Miscellaneos:SOAP(简单对象访问协议) A,返回顶部 1, 简单对象访问协议是交换数据的一种协议规范,是一种轻量的.简单的.基于XML(标准通用标记语言下的一个子集)的 ...
- WS-Security
ylbtech-Miscellaneos: WS-Security A,返回顶部 1, WS-Security (Web服务安全) 是一种提供在Web服务上应用安全的方法的网络传输协议. 2004年4 ...
- 企业架构(Enterprise Architecture)
ylbtech-Miscellaneos: 企业架构(Enterprise Architecture) A,返回顶部 1, 简称EA.是指对企业事业信息管理系统中具有体系的.普遍性的问题而提供的通用解 ...
- ASP.NET中 WebForm 窗体控件使用及总结【转】
原文链接:http://www.cnblogs.com/ylbtech/archive/2013/03/06/2944675.html ASP.NET中 WebForm 窗体控件使用及总结. 1.A, ...
随机推荐
- Majority Element——算法课上的一道题(经典)
Given an array of size n, find the majority element. The majority element is the element that appear ...
- 【转载】Python: Enum枚举的实现
转自:http://www.cnblogs.com/codingmylife/archive/2013/05/31/3110656.html 从C系语言过来用Python,好不容易适应了写代码不打 ...
- 前端读者 | 百度前端编码规范(CSS)
本文来自:百度FEX 1 前言 CSS作为网页样式的描述语言,在百度一直有着广泛的应用.本文档的目标是使CSS代码风格保持一致,容易被理解和被维护. 虽然本文档是针对CSS设计的,但是在使用各种CSS ...
- centos7.5安装seafile
1.配置yum源 [root@localhost yum.repos.d]# uname -r3.10.0-693.el7.x86_64 [root@localhost yum.repos.d]# c ...
- tmpfs文件系统
centos 7测试OK 创建挂载点,挂载 mkdir -p /run/testdirmount -nt tmpfs -o size=500m,mode=755 tmpfs /run/testdir
- React Native 0.56.1初始化项目运行出现错误(Module `AccessibilityInfo` does not exist in the Haste module map)
当使用react-native init myApp初始化项目时,出现以下错误 出现以上错误的原因是因为0.56.1版本初始化项目就有问题,请见 https://github.com/facebook ...
- mysql 允许远程IP连接, 并查看所有用户的所有权限
添加mysql用户 http://my.oschina.net/u/1179414/blog/202377 允许远程ip连接 GRANT select,insert,update,delete ON ...
- 转:深入了解Windows句柄
深入了解Windows句柄到底是什么 转:http://blog.csdn.net/wenzhou1219/article/details/17659485 总是有新入门的Windows程序员问我Wi ...
- POJ 1679 The Unique MST 【最小生成树/次小生成树模板】
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22668 Accepted: 8038 D ...
- 推荐Maven的两个仓库
概述 推荐两个maven的仓库,可用于查找依赖,下载jar包. 正文 mvnrepository 这个仓库用来检索依赖.下载jar包:网址:http://mvnrepository.com/ 仓库的主 ...