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

推荐订阅源

T
Threatpost
Forbes - Security
Forbes - Security
P
Palo Alto Networks Blog
Scott Helme
Scott Helme
S
Securelist
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Visual Studio Blog
P
Proofpoint News Feed
大猫的无限游戏
大猫的无限游戏
博客园 - 三生石上(FineUI控件)
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
AWS News Blog
AWS News Blog
P
Privacy & Cybersecurity Law Blog
L
LINUX DO - 热门话题
L
Lohrmann on Cybersecurity
Last Week in AI
Last Week in AI
Engineering at Meta
Engineering at Meta
Spread Privacy
Spread Privacy
博客园_首页
T
Tor Project blog
The Cloudflare Blog
博客园 - 聂微东
罗磊的独立博客
Cyberwarzone
Cyberwarzone
腾讯CDC
T
The Exploit Database - CXSecurity.com
Google DeepMind News
Google DeepMind News
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
P
Privacy International News Feed
D
Darknet – Hacking Tools, Hacker News & Cyber Security
U
Unit 42
A
Arctic Wolf
C
Cybersecurity and Infrastructure Security Agency CISA
A
About on SuperTechFans
G
GRAHAM CLULEY
K
Kaspersky official blog
月光博客
月光博客
Microsoft Security Blog
Microsoft Security Blog
T
Tenable Blog
L
LINUX DO - 最新话题
酷 壳 – CoolShell
酷 壳 – CoolShell
IT之家
IT之家
TaoSecurity Blog
TaoSecurity Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
MongoDB | Blog
MongoDB | Blog
T
The Blog of Author Tim Ferriss
C
Cyber Attacks, Cyber Crime and Cyber Security
博客园 - 司徒正美
O
OpenAI News
Recent Announcements
Recent Announcements

博客园 - 阳春三月

工作态度两则 【转载】看图测性格 【转载】乔海燕-外科医生的手术技巧 记念 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 被用某个类型实例化后,泛型就成了一种新的确定的类型,记住这点就够了。