Recently, I was diving in lambda expressions, and there was a certain functionality that I wanted to learn, but simply could not create heads or tails.
Suppose there is the following logic in my code:
List<A> foo; // assuming this is populated string[] bar = foo.Select<A,string>(x => x.StringProperty).ToArray<string>();
Now I want to possibly distract all this operation in the handler method so that I can do this:
string[] bar = MakeArray<A,string>(foo, x => x.StringProperty); int[] foobar = MakeArray<A,int>(foo, x => x.IntegerProperty);
How can I start writing this method? I envision a signature declaration somehow like:
U[] MakeArray<T,U>( ) where T : IEntity {}
but I donβt know how to indicate that I expect a lambda expression as an argument to a method, and how it exactly translates to the method body.
Can someone show me how to create the MakeArray() function above? I am pretty sure that as soon as I see how to do this, I can pick it up from there.
EDIT
As pointed out in the comments, MakeArray() needs a reference to IEnumerable<> . Updated to reflect this.
generics c # lambda
Richard Neil Ilagan Mar 22 '11 at 19:30 2011-03-22 19:30
source share