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

推荐订阅源

N
News and Events Feed by Topic
Stack Overflow Blog
Stack Overflow Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
量子位
Apple Machine Learning Research
Apple Machine Learning Research
B
Blog RSS Feed
F
Full Disclosure
aimingoo的专栏
aimingoo的专栏
云风的 BLOG
云风的 BLOG
小众软件
小众软件
Hugging Face - Blog
Hugging Face - Blog
A
About on SuperTechFans
G
Google Developers Blog
U
Unit 42
腾讯CDC
雷峰网
雷峰网
爱范儿
爱范儿
H
Help Net Security
Engineering at Meta
Engineering at Meta
Vercel News
Vercel News
Cloudbric
Cloudbric
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
Secure Thoughts
有赞技术团队
有赞技术团队
Jina AI
Jina AI
L
LINUX DO - 最新话题
Martin Fowler
Martin Fowler
C
Cybersecurity and Infrastructure Security Agency CISA
V
Visual Studio Blog
TaoSecurity Blog
TaoSecurity Blog
F
Fortinet All Blogs
S
Security @ Cisco Blogs
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
IT之家
IT之家
T
The Blog of Author Tim Ferriss
K
Kaspersky official blog
N
News | PayPal Newsroom
美团技术团队
月光博客
月光博客
PCI Perspectives
PCI Perspectives
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Google Online Security Blog
Google Online Security Blog
L
LINUX DO - 热门话题
Help Net Security
Help Net Security
Recent Announcements
Recent Announcements
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
罗磊的独立博客
酷 壳 – CoolShell
酷 壳 – CoolShell
Latest news
Latest news
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com

博客园 - JerryShi

使用流模式传输大型数据 对象序列化之 替换(Surrogate) 使用 IExtensibleDataObject 进行往返式序列化 使用 NetDataContractSerializer 共享类型 WCF可序列化方式 序列化 VS 编码 自定义 Behavior 操作行为 之 事务 服务行为 之 元数据 服务行为 之 并发与实例化 基本的WCF编程(实例) WCF Behaviors(行为) WCF 体系结构 WCF服务 使用netTcpBinding 无法打开默认数据库 SharePoint 创建SSP时出现找不到 Windows NT 用户或组 的异常 SQL Server 2008 Mirror 自定义属性Attribute(三) 自定义属性Attribute(二)
泛型(一)
JerryShi · 2009-12-06 · via 博客园 - JerryShi

泛型的目的:提高代码的可重用性和类型安全性。

泛型引语

重用性的表现,在最初接触开发的时候,总会有从数据库中取值并写如对象中,通常实现的方式是利用这样的代码: EntityClass obj = new EntityClass(); obj.ID = dataReader[0]==null?string.Empty:dataReader[“fieldname_ID”].toString();而实体类总是由多个字段组成的,因此实体类有多少个字段,我们就不的写多少行此类的代码,自从有了泛型,这样的工作就有缩减,可以通过下面的代码来实现重用:

private staticT GetDataSourceValue<T>(object dataSource, string fieldName, TdefaultValue)

             {

                    T result = defaultValue;

                    if (dataSource is DataRow)

                           result = (T)((DataRow)dataSource)[fieldName];

                    else

                           result = (T)((DataReader)dataSource)[fieldName];

                    returnresult;

             }

泛型术语

约束:对泛型类型参数的约束。

泛型方法:只有在方法具有自己的类型参数列表时,此方法才能称为泛型方法。

泛型类型和继承

泛型类型本质上还是一个类型,所以其可以从其他任何类型派生,也可以被继承和实现。指定类型实参,不会与层次发生关系。

代码示例:

Node

public class Node
{
protected Node _next;public Node(Node next)
{
this._next = next;
}
}
public class GenericNode<T> : Node
{
public T _data;public GenericNode(T data)
:
this(data, null)
{ }
public GenericNode(T data, Node next)
:
base(next)
{
this._data = data;
}
public override string ToString()
{
return this._data.ToString() + "|" + (this._next != null ? this._next.ToString() : string.Empty);
}
}

泛型类型同一性

可以从泛型类型进行派生,基类指定了类型实参。为了简化下面这样的代码:

List<DateTime> list = newList<DateTime>(); 我们会定义这样一个类:

public seal class DateTimeList :List<DateTime> { } ;然后可以进行这样的定义:

DateTimeList list = new DateTimeList(); 但是需要注意的是这样做会丢掉代码的同一性和相等性,即: typeof(DateTimeList) != typeof(List<DateTime>) 。

为解决此问题,我们可以使用using指令,在代码文件首部定义此转移表达式:

usingDateTimeList = System.Collection.Generic.List<System.DateTime> ;并且typeof(DateTimeList)== typeof(List<DateTime>);

code explosion

CLR进行编译时需要为不同的方法/类型生成本地代码,若每一种泛型参数类型都需要生成一份代码,则程序集会增加很大,损害性能,这被称为code explosion

改善方法:

  1. 假定为一个特定的类型实参调用了一个方法,则当前的AppDomain中的相同类型的实参都会使用此方法。
  2. CLR认为所有的引用类型的实参是完全相同的。由于引用类型的变量实际是指向堆上的对象的指针。其操作都一样。