public static string CommaSeparate( this IEnumerablesource, Func return string.Join(",", source.Select(s => func(s).ToString()).ToArray()); }func) {
I appreciate that a Lambda expression matches a Func delegate in terms of Input maps to Output.
So why does the Lambda in the Select method show s => func(s).ToString(); rather than s => s.ToString();
Thanks
Static Extension Method using Func and Lambda
Given the following:-
VulpesPosted Apr 1, 2013, 9:12 AM
The Select method is not bothered whether you pass it an actual delegate or a lambda expression because the latter is always convertible to a compatible delegate.
Guest UserPosted Apr 1, 2013, 8:23 AM
Guest UserPosted Mar 31, 2013, 8:51 AM
One last question on this topic please if I may...
I found an example of:-
public static List
VulpesPosted Mar 30, 2013, 4:44 PM
So, if you have:
s => func(s).ToString()
the compiler infers TResult to be string.
Guest UserPosted Mar 29, 2013, 3:19 PM
What threw me was the usage of the Func
And the fact that the method then uses the Select Extension method which again takes Func
There are a lot of Func delegates flying about in this one method which confused me...
I now appreciate that (s) => func(s).ToString(); is the Lambda expression used in the Select Extension method's parameter to correspond\map to the func parameter of the CommaSeperate Extension method.
PS I assume the Type of TResult that is inferred to be of Type int, is hidden from view? as id expect to see it before the call to the ToString() method!?
(s) => func(s).ToString();
Regards...
VulpesPosted Mar 29, 2013, 12:36 PM
However, the point I was trying to make is that if the input and output types are the same, this doesn't necessarily mean that the input parameter has to be transformed in some way; it can simply be passed out again unmodified. The position would then be similar to having a parameter of type T rather than Func
The use of Func
Guest UserPosted Mar 29, 2013, 11:39 AM
I thought that syntax was, input goes to result? which corresponds\maps
with Func
VulpesPosted Mar 29, 2013, 11:16 AM
http://msdn.microsoft.com/en-us/library/system.linq.enumerable_methods(v=vs.100).aspx
As you'll see from this list Select is one of them.
Of course, the Func delegate is completely general and if you use Func
Guest UserPosted Mar 29, 2013, 8:48 AM
Do you know of any other examples in or out of the .NET Framework that use the Lambda mapping syntax:-
(Input) => someMethod(Input) i.e s => func(s);
Is the above only possible due to the Lambda being used within the Select method in which itself has a Func delegate?
As opposed to the more common form when invoking a Func delegate with a Lambda?
(Input) => (Output\Result) i.e s => s;
Regards
VulpesPosted Mar 29, 2013, 5:51 AM
Guest UserPosted Mar 29, 2013, 5:07 AM
The Type parameters for Func
So given s => func(s).ToString();
Why does s hold and pass User to Func
VulpesPosted Mar 28, 2013, 7:03 PM
It's transforming the members first using Func and then comma separating the results.
Here's a simple example which takes an array of ints, doubles them and then comma separates them:
The output is:
2,4,6