110,538
社区成员
发帖
与我相关
我的任务
分享
static void Main(string[] args)
{
var lst = new[] {1, 2, 3};
T AsIs<T>(T input)
{
return input;
}
//能否定义出【一个】变量asIs使其能在以下三句话中代替泛型局部函数AsIs?
//Func<?, ?> asIs = i => i;
var lst2 = lst.Select(AsIs);
var lst3 = lst.Cast<string>().Select(AsIs);
var lst4 = lst.Cast<object>().Select(AsIs);
}
private static void Main(string[] args)
{
int[] numArray = new int[] { 1, 2, 3 };
IEnumerable<int> enumerable = Enumerable.Select<int, int>(numArray, new Func<int, int>(<>c.<>9.<Main>g__AsIs|0_0<int>));
IEnumerable<string> enumerable2 = Enumerable.Select<string, string>(Enumerable.Cast<string>(numArray), new Func<string, string>(<>c.<>9.<Main>g__AsIs|0_0<string>));
IEnumerable<object> enumerable3 = Enumerable.Select<object, object>(Enumerable.Cast<object>(numArray), new Func<object, object>(<>c.<>9.<Main>g__AsIs|0_0<object>));
}
var lst2 = lst.Select(x=>x);
你会认为select的参数是一个方法,
那么写成
var lst2 = lst.Select(new Func<int, int>(x=>x));
new Func<int, int>(x=>x)也是一个对象啊,一个变量
Func<T, T> AsIs = x => x;
Func已经是泛型了,就是说用泛型再定义一个泛型方法
用“变量”代替“方法”,变量其实就是委托对象了,如下样子
public delegate Func<T,T> genericDel<T>(T input);
因为selector需要Func函数,于是这个委托返回Func类型。但接下来,我是不会弄了
其实可以这样
Func<int,int> AsIs1 = x => x;
Func<string, string> AsIs2 = x => x;
Func<object, object> AsIs3 = x => x;
var lst = new int[] { 1, 2, 3 };
var lst2 = lst.Select(AsIs1);
//..........
不就是变量了吗