C#泛型编程知道原则(八、九)

来源:互联网 发布:RecyclerView 添加数据 编辑:程序博客网 时间:2024/05/22 00:54

Item 8: Don’t Use Constructed Types as Type Arguments不要把构造类型作为类型参数

虽然你拥抱了泛型的光彩,依然需要确保不要走极端。因为有可能在选择使用了泛型后,处理过程反而没有原本的优雅。

如:

public class MyComplexType<T, U> { }

public class MyType2<T, U, V> { }

public class MyType3<T, U> { }

public class TestClass

{

public void foo()

 {

MyComplexType<MyType2<int, string, double>, MyType3<string, string>> x =

new MyComplexType<MyType2<int, string, double>, MyType3<string, string>>();

}

}

从该例子可以看出,这样做极大的影响了程序的可读性。

Item 9: Don’t Use Too Many Type Parameters不要使用太多的类型参数

一般来说,类型参数不要超过2个。因为使用的类型参数越多,就越难使用、维护和理解。

 
原创粉丝点击