Search code examples
c#delegatesargumentsfunc

c# new Func as an argument


I have string.Select set like this:

Func<string, int> selector = str => str.Length;
var a = input.Split(splitChars).Select(selector).ToArray()

Now is there a way to put the Func delegate right into Select as a new argument to spare that one line?

Something like this (but it does not work that way):

var a = input.Split(splitChars).Select(new Func<string, int> = str => str.Length).ToArray()

Thanks for your help


Solution

  • You can define a delegate function inside the select like this:

    var a = input.Split(splitChars).Select(c => c.Length).ToArray();
    

    You can input all delegates of form Func<string, T> that way.