Search code examples
c#delegatespartial-application

C# define function with partial application as delegate


Consider the following method:

int Foo(string st, float x, int j)
{
    ...
}

Now I want to wrap it in a delegate of type Func<float, int> by providing values for the parameters st and j. But I don't know the syntax. Can someone help?

This is the idea (might look a bit Haskell-ish):

Func<float, int> myDelegate = new Func<float, int>(Foo("myString", _ , 42));
// by providing values for st and j, only x is left as a parameter and return value is int

Solution

  • This should do the trick:

    Func<float, int> f = (x) => { return Foo("myString", x, 42); };
    

    Partially applying functions the way you want to do it is currently only possible in F#, not in C#.