一:泛型方法
public static TResult MySelect<TSource, TResult>(this TSource source, Func<TSource, TResult> selector)
{
return selector(source);
} 调用方式1:先设定泛型类型
test t = new test();
t.Id = 2;
t.MySelect<test, int?>(a=>a.uid);
调用方式2:也可以先不规定泛型,c#支撑类型推断
test t = new test();
t.nuthor = "m";
t.MySelect(a => a.nuthor); 这种方式TRsult也就是返回值就是一个string类型
本文详细介绍了C#中泛型方法的概念、定义及使用方法,并通过实例展示了如何利用泛型方法解决实际编程问题。
993

被折叠的 条评论
为什么被折叠?



