C# frameword 泛型引入 场景:相似的逻辑只是传入参数类型或返回的类型不同 List Dictionary Actionobjecteventargs 方式:object但会有类型转换对于值类型会有性能损耗泛型:定义时使用类型占位符T也可以用其他字母-----类型参数 泛型思想:延迟类或方法中的数据类型的指定直到程
//定义一个泛型类,类名后加上
//使用泛型类和泛型方法,用具体类型代替T
MyGenericClass
MyGenericClass
//泛型约束示例,使用where关键字指定约束条件
public class MyGenericClass
public interface IMyInterface { //... }
public class MyGenericClass
public class MyGenericClass<T, U> where T : U { //T必须是U的子类或实现U接口 //... }
原文地址: https://www.cveoy.top/t/topic/bo40 著作权归作者所有。请勿转载和采集!