简体   繁体   中英

Can I define a delegate as the restriction of a function in c#?

Suppose i have two functions: f(x,y) and g(x). In mathematics, if you have a fixed y, say y_fix, you can define:

g(x) := f(x,y_fix)

Now suppose that g() is ac# delegate, and f is an implemented function. Can I define g(x) in an analogous way? eg

public delegate double SingleVariableFunction(double x);
public class SomeClass
{
    SingleVariableFunction g;
    double y_fix;

    public SomeClass()
    {
        y_fix = 2;
        g = f(  ,y_fix);    /// Is this possible somehow?
    }

    public double f(double x, double y)
    {
        return x + y;   
    }
}

It's called partial application .

In C# you can construct g with lambda expression:

g = x => f(x, y_fix);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM