惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
The Blog of Author Tim Ferriss
Know Your Adversary
Know Your Adversary
P
Palo Alto Networks Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
K
Kaspersky official blog
L
LINUX DO - 热门话题
P
Proofpoint News Feed
P
Privacy & Cybersecurity Law Blog
Google DeepMind News
Google DeepMind News
Attack and Defense Labs
Attack and Defense Labs
Cisco Talos Blog
Cisco Talos Blog
AI
AI
L
LINUX DO - 最新话题
H
Heimdal Security Blog
Hacker News: Ask HN
Hacker News: Ask HN
Webroot Blog
Webroot Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The GitHub Blog
The GitHub Blog
I
Intezer
Blog — PlanetScale
Blog — PlanetScale
有赞技术团队
有赞技术团队
S
Securelist
博客园_首页
IT之家
IT之家
Schneier on Security
Schneier on Security
博客园 - 叶小钗
罗磊的独立博客
WordPress大学
WordPress大学
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
MongoDB | Blog
MongoDB | Blog
P
Proofpoint News Feed
阮一峰的网络日志
阮一峰的网络日志
A
Arctic Wolf
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
W
WeLiveSecurity
The Register - Security
The Register - Security
D
DataBreaches.Net
S
Security @ Cisco Blogs
Security Archives - TechRepublic
Security Archives - TechRepublic
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
腾讯CDC
Recorded Future
Recorded Future
NISL@THU
NISL@THU
N
News and Events Feed by Topic
T
Tailwind CSS Blog
N
News and Events Feed by Topic
Cyberwarzone
Cyberwarzone
T
Tor Project blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com

博客园 - 超子

My Development PC - II My Development PC - I 为Migrator.net制作一个可自动生成版本号的项目模板 Hone C# III La Maison en Petits Cubes Hone C# 卡壳 游戏开发学习 02 游戏开发学习 01 游戏开发学习 00 C#次大数的函数 语言学习开篇 - 写在前面 送同志们留念:动态指示条(Activity indicators) 送同志们留念:看看我们的MSDN China 都为我们作了些什么 热点杂记:Atlas Control Toolkits 更新 热点杂记:Asp.net 的演示视频 VS2005 中的 Template 热点杂记:Scott Guthrie 中文博客开通 送同志们留念:各种符号的英文名称
Hone C# II
超子 · 2009-03-26 · via 博客园 - 超子

泛型 Generic - 即通用类型
------------------------------------
C#是强类型语言,在享受强类型带来的诸多好处的同时你必须在逻辑设计的同时顾及类型(type),即便你可以通过把对象boxing成object来将相同逻辑的方法、列表对象或delegate抽象出来,但你不得不考虑在使用他们时的unboxing或强制转换,而这会带来一些性能的负担和潜在的风险。籍此,C#2.0后引入了Generic(泛类型,通用类型),也就是把类型(type)设置成未知数T,这样就可以在书写逻辑的时候忽略Type只关注

逻辑,在具体调用的时候再指定这个T。这样一来不但加大了代码的重用性而且还能保证这些方法、列表、delegate等是强类型的,没有了boxiing/unboxing和强制转换,一切就这么完美了(或者说趋于完美了)。

有了泛型(Generic)List<T>,Func<TResult>...,Action<>.... 看起来就很自然,而利用这三个东西设计出来的框架就狂简炼狂强大,比如LINQ。List<T>没啥好说的,算是凑数的,Func<TResult>和Action<>为我们固化了两类方法的delegate,Func返回一个结果,Action不返回,他们各自最多可接收4个参数,这两代表可以代表绝大多数methods,绝对强大绝对牛叉,如果你不能理解,没关系我也不能理解,但是把任何东西对象化都是件很爽的事情,这当然也包括methods,先姑且这么认为吧。

注:Lambda Expression 是 .net frameworks 3.0后的东西。除此之外以上的都是2.0的了。

Code