class A
{
private int p;
public A(int a)
{
p = a;
}
}
int[] n = { 1, 2, 3, 4, 5 };
how to make an array of A initialized with values from n using lambda. Its ok to use lambda for that?
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();
But you can use the explicit LINQ syntax where you type out the lambda.
n.Select(i => new A(i)).ToArray();
Enumerable.ToArray<T> creates a Buffer<T> and not a List<T> internally. Nevertheless there might be one extra copy step in ToArray<T> compared to ToList<T> if the final array length is not a power of 2. But until profiling has proven that is your problem you don't need to worry about it.
i => new A(i). To use the lambda you must call it or pass it to a method that calls the lambda. LINQ contains many suitable methods for dealing with collections, many of them is preferably called with lambdas as arguments. Your question would make more sense if you replaced "lambda" with LINQ.