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

推荐订阅源

V
Vulnerabilities – Threatpost
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Engineering at Meta
Engineering at Meta
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Blog — PlanetScale
Blog — PlanetScale
aimingoo的专栏
aimingoo的专栏
雷峰网
雷峰网
Microsoft Azure Blog
Microsoft Azure Blog
V
Visual Studio Blog
F
Fortinet All Blogs
C
CERT Recently Published Vulnerability Notes
Spread Privacy
Spread Privacy
月光博客
月光博客
L
LINUX DO - 热门话题
C
Cisco Blogs
P
Proofpoint News Feed
C
Cyber Attacks, Cyber Crime and Cyber Security
爱范儿
爱范儿
B
Blog
P
Privacy International News Feed
Know Your Adversary
Know Your Adversary
The Cloudflare Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cyberwarzone
Cyberwarzone
J
Java Code Geeks
罗磊的独立博客
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Cisco Talos Blog
Cisco Talos Blog
T
Tor Project blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园 - 司徒正美
酷 壳 – CoolShell
酷 壳 – CoolShell
Simon Willison's Weblog
Simon Willison's Weblog
量子位
H
Help Net Security
The Register - Security
The Register - Security
L
LangChain Blog
GbyAI
GbyAI
Vercel News
Vercel News
S
Schneier on Security
T
Threatpost
T
Threat Research - Cisco Blogs
有赞技术团队
有赞技术团队
Y
Y Combinator Blog
阮一峰的网络日志
阮一峰的网络日志
C
Check Point Blog
MongoDB | Blog
MongoDB | Blog
Recorded Future
Recorded Future
G
GRAHAM CLULEY
MyScale Blog
MyScale Blog

博客园 - 阳春三月

工作态度两则 【转载】看图测性格 【转载】乔海燕-外科医生的手术技巧 记念 33-101 使用 MyMeta 组件获取数据库结构 获取 SQL Server 2005 中的元数据 看博客园文章的一点感想 事件与委托有别, delegate 与 Delegate 相异 - 阳春三月 伟大的 PL/Sql Developer——数据库结构比较 新年礼物:推荐小工具 C#属性的实质还是方法 abstract、virtual、override 和 new 在 IE 中承载 Winform 应用程序 安装 VS2008 SP1 后需要给 TFS2008 装 SP1 更新下载程序集缓存 尼康 P5100 的几张片子 吃油饼的男人 Linq error: Sequence contains no elements TFS 问题两则
泛型小经验
阳春三月 · 2009-07-21 · via 博客园 - 阳春三月

    1. 泛型类可以继承自非泛型类,类似 public class MyArray<T> : ArrayList 或 public partial class Form1<T> : Form 是可行的。

    2. 泛型在集合的管理中非常好用,像 List<T> 毋庸置疑是最常用的集合。但当两个集合的 T 具有派生关系时,子类集合不能转换成父类集合,显式或隐式转换都不行。比如定义如下父子两个类:
        public class ParentClass { }
        public class ChildClass : ParentClass { }
    下面这句话会发生编译错误:
        List<ParentClass> ps = new List<ChildClass>() as List<ParentClass>;
    编译器提示:
    无法通过引用转换、装箱转换、取消装箱转换、包装转换或 Null 类型转换将类型“System.Collections.Generic.List<ChildClass>”转换为“System.Collections.Generic.List<ParentClass>”       

    这似乎很不合理,我把子类对象集合转换成父类对象集合,“父类引用指向子类对象”,平时经常这么干为啥这次就不行了呢?
    仔细想一下,因为我们用的是 List<T> 类型,而不是某个类似 List(T) 的方法。在程序运行时,JIT 编译器会根据实际情况把 T 替换成相应的类型,替换后的 List<ParentClass> 跟 List<ChildClass> 是不同的类型,而且也没有派生关系,自然就不能转换了。总之,泛型的 T 被用某个类型实例化后,泛型就成了一种新的确定的类型,记住这点就够了。