具有lambda类型的init数组 [英] Init array of type with lambda
本文介绍了具有lambda类型的init数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
class A
{
private int p;
public A(int a)
{
p = a;
}
}
int[] n = { 1, 2, 3, 4, 5 };
如何使用lambda使A数组初始化为n的值. 可以使用lambda吗?
how to make an array of A initialized with values from n using lambda. Its ok to use lambda for that?
推荐答案
我更喜欢LINQ查询语法(幕后有一个lambda,但隐藏在语法糖之后).
I prefer the LINQ query syntax (there is a lambda behind the scenes but hidden behind syntactic sugar).
(
from i in n
select new A(i)
).ToArray();
但是您可以在键入lambda的地方使用显式LINQ语法.
But you can use the explicit LINQ syntax where you type out the lambda.
n.Select(i => new A(i)).ToArray();
这篇关于具有lambda类型的init数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文