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

推荐订阅源

Attack and Defense Labs
Attack and Defense Labs
The GitHub Blog
The GitHub Blog
C
Check Point Blog
博客园_首页
MongoDB | Blog
MongoDB | Blog
N
Netflix TechBlog - Medium
F
Full Disclosure
Microsoft Security Blog
Microsoft Security Blog
爱范儿
爱范儿
Recent Announcements
Recent Announcements
阮一峰的网络日志
阮一峰的网络日志
G
GRAHAM CLULEY
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Threat Research - Cisco Blogs
C
Cybersecurity and Infrastructure Security Agency CISA
V
Vulnerabilities – Threatpost
K
Kaspersky official blog
博客园 - 司徒正美
S
Schneier on Security
T
The Exploit Database - CXSecurity.com
Project Zero
Project Zero
云风的 BLOG
云风的 BLOG
Cisco Talos Blog
Cisco Talos Blog
Know Your Adversary
Know Your Adversary
雷峰网
雷峰网
V
V2EX - 技术
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Spread Privacy
Spread Privacy
罗磊的独立博客
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Security Affairs
SecWiki News
SecWiki News
Schneier on Security
Schneier on Security
O
OpenAI News
Jina AI
Jina AI
PCI Perspectives
PCI Perspectives
Cyberwarzone
Cyberwarzone
Y
Y Combinator Blog
Apple Machine Learning Research
Apple Machine Learning Research
B
Blog RSS Feed
I
InfoQ
D
Docker
P
Palo Alto Networks Blog
Recorded Future
Recorded Future
M
MIT News - Artificial intelligence
博客园 - Franky
B
Blog
Scott Helme
Scott Helme
博客园 - 叶小钗
D
DataBreaches.Net

博客园 - 洛客

IOS SDK 兼容性 研究 中国地址的英文翻译 重构的概念 - 洛客 - 博客园 WCF RIA Services DomainService life-cycle and adding Transactions Silverlight 3 RIA服务编程中使用自定义方法及服务操作 HP大中华区总裁孙振耀退休感言 Session莫名丢失的原因及解决办法 在SQL Server 2005中实现 Server.UrlEncode、Server.UrlDecode 商品條碼 让包装纸箱增加网店服务的附加值 男人30岁需要完成的事! 結婚照 我要结婚了 C#中如何实现对象拷贝 四舍五入 与 四舍六入五成双 网站优化——静态页面和动态页面的区别 - 洛客 网站优化—百度和google的一些优化特点 汕頭地方方言 Speed Up Your Site with the Improved View State in ASP.NET 2.0
C#泛型
洛客 · 2009-12-10 · via 博客园 - 洛客

C#泛型类与结构
C#除可单独声明泛型类型(包括类与结构)外,也可在基类中包含泛型类型的声明。但基类如果是泛型类,它的类型参数要么已实例化,要么来源于子类(同样是泛型类型)声明的类型参数。
class C<U, V> {} //合法
class D: C<string,int>{} //合法
class E<U, V>: C<U, V> {} //合法
class F<U, V>: C<string, int> {} //合法
class G : C<U, V> { } //非法

泛型类型的成员
class C<V>{
public V f1; //声明字段
public D<V> f2; //作为其他泛型类型的参数
public C(V x) {
this.f1 = x;
}
}
泛型类型的成员可以使用泛型类型声明中的类型参数。但类型参数如果没有任何约束,则只能在该类型上使用从System.Object继承的公有成员。

泛型接口
interface IList<T> {
T[] GetElements();
}
interface IDictionary<K,V> {
void Add(K key, V value);
}
// 泛型接口的类型参数要么已实例化,
// 要么来源于实现类声明的类型参数
class List<T> : IList<T>, IDictionary<int, T> {
public T[] GetElements() { return null; }
public void Add(int index, T value) { }
}
泛型委托
delegate bool Predicate<T>(T value);
class X {
static bool F(int i) {...}
static bool G(string s) {...}
static void Main() {
Predicate<string> p2 = G;
Predicate<int> p1 = new Predicate<int>(F);
}
}
泛型委托支持在委托返回值和参数上应用参数类型,这些参数类型同样可以附带合法的约束。
泛型方法简介
• C#泛型机制只支持“在方法声明上包含类型参数”——即泛型方法
• C#泛型机制不支持在除方法外的其他成员(包括属性、事件、索引器、构造器、析构器)的声明上包含类    型参数,但这些成员本身可以包含在泛型类型中,并使用泛型类型的类型参数
• 泛型方法既可以包含在泛型类型中,也可以包含在非泛型类型中
泛型方法的声明与调用
//不是泛型类,是一个具体的类,这个类不需要泛型类型的实例化
public class Finder {
// 但是是一个泛型方法,请看泛型方法的声明,参数要求泛型化
public static int Find<T> ( T[] items, T item) {
for(int i=0;i<items.Length;i++){
if (items[i].Equals(item)) { return i; }
}
return -1;
}
}
// 泛型方法的调用<int>不是放到Finder后面,而是放在Find后面。
int i=Finder.Find<int> ( new int[]{1,3,4,5,6,8,9}, 6);

泛型方法的重载
class MyClass {
void F1<T>(T[] a, int i); // 不可以构成重载方法
void F1<U>(U[] a, int i);
void F2<T>(int x); //可以构成重载方法
void F2(int x);
//两句申明一样,where字句,T继承A,泛型参数必需要继承A

void F3<T>(T t) where T : A;

//不可以构成重载方法
void F3<T>(T t) where T : B;
}
泛型方法的重写
abstract class Base
{
public abstract T F<T,U>(T t, U u) where U: T;
public abstract T G<T>(T t) where T: IComparable;
}
class Derived: Base{
//合法的重写,约束被默认继承,只需要写方法的签名
public override X F<X,Y>(X x, Y y) { }
//非法的重写,指定任何约束都是多余的
//重写的时候,不能写约束,也不添加新的约束,只能继承父类的约束。
public override T G<T>(T t) where T: IComparable {}
}
泛型约束简介
• C#泛型要求对“所有泛型类型或泛型方法的类型参数”的任何假定,都要基于“显式的约束”,以维护
C#所要求的类型安全。
• “显式约束”由where子句表达,可以指定“基类约束”,“接口约束”,“构造器约束”“值类型/引用类型约束”共四种约束。
• “显式约束”并非必须,如果没有指定“显式约束”,泛型类型参数将只能访问System.Object类型中的公有方法。
基类约束
class A { public void F1() {…} }
class B { public void F2() {…} }
class C<S,T>
where S: A // S继承自A
where T: B // T继承自B
{
// 可以在类型为S的变量上调用F1,
// 可以在类型为T的变量上调用F2
….
}
接口约束
interface IPrintable { void Print(); }
interface IComparable<T> { int CompareTo(T v);}
interface IKeyProvider<T> { T GetKey(); }
class Dictionary<K,V>
where K: IComparable<K>
where V: IPrintable, IKeyProvider<K>
{
// 可以在类型为K的变量上调用CompareTo,
// 可以在类型为V的变量上调用Print和GetKey
….
}
构造器约束
class A { public A() { } }
class B { public B(int i) { } }
class C<T>
where T : new()
{
//可以在其中使用T t=new T();
….
}
C<A> c=new C<A>(); //可以,A有无参构造器
C<B> c=new C<B>(); //错误,B没有无参构造器
值类型/引用类型约束
public struct A { … }
public class B { … }
class C<T>
where T : struct
{
// T在这里面是一个值类型

}
C<A> c=new C<A>(); //可以,A是一个值类型
C<B> c=new C<B>(); //错误,B是一个引用类型

总结
• C#的泛型能力由CLR在运行时支持,它既不同于C++在编译时所支持的静态模板,也不同于Java在编译器层面使用“搽拭法”支持的简单的泛型。
• C#的泛型支持包括类、结构、接口、委托共四种泛型类型,以及方法成员。
• C#的泛型采用“基类, 接口, 构造器, 值类型/引用类型”的约束方式来实现对类型参数的“显式约束”,它不支持C++模板那样的基于签名的隐式约束。