您的位置:首页 > 编程语言 > C#

C#泛型编程基础知识总结[转]

2008-07-24 15:46 288 查看
在项目中通过对项目不断更深的认识,运用了设计模式,就难免不运到开箱和装箱操作,通常的开箱和装箱操作对系统的性能有一定的影响。为了解决这一个问题,其中一种解决方案是运用泛型来解决。下面是C#2.0泛型的简单介绍和使用,便于在项目中灵活运用。

一、C#泛型演示

class Stack<T>

class C<V>

interface IList<T>

interface IDictionary<K,V>

class List<T> : IList<T>, IDictionary<int, T>

delegate bool Predicate<T>(T value);

class X

public class Finder

// 泛型方法的调用

class MyClass

abstract class Base

class Derived: Base

class A

class B

class C<S,T>

where S: A // S继承自A

where T: B // T继承自B

interface IPrintable

interface IComparable<T>

interface IKeyProvider<T>

class Dictionary<K,V>

where K: IComparable<K>

where V: IPrintable, IKeyProvider<K>

class A

C<B> c=new C<B>(); // 错误,B 没有无参构造器

where T : new()

// 可以在其中使用T t=new T();

C<A> c=new C<A>(); // 可以,A 有无参构造器

十八、值类型/引用类型约束

public struct A

C<B> c=new C<B>(); //错误,B 是一个引用类型
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: