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 是一个引用类型
一、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 是一个引用类型
相关文章推荐
- 全国计算机技术与软件专业技术资格(水平)考试【软件评测师】-考试内容总结(五)计算机网络基础知识
- JavaScript总结(二:基础知识)
- Web前端开发基础知识--部分总结
- java基础知识总结2三种常见的结构
- Java基础知识总结(二)
- 性能测试基础知识总结
- Java基础知识总结(1)
- javascript中BOM部分基础知识总结
- android linux 基础知识总结(3)
- JDBC基础知识总结
- 我的Java基础知识总结1
- DRP 基础知识总结(一)
- js基础知识总结(2016.11.1)
- C语言基础知识总结(1)-基本数据类型
- JavaScript学习总结(7)——JavaScript基础知识汇总
- c++基础小知识归纳总结
- JAVA基础知识精华总结
- 总结一下WinPcap编程(基础知识且不含代码例程)
- 关于css3动画基础知识总结
- javaScript基础知识学习总结