Storing a method as a member variable of a class

后端 未结 4 499
野趣味
野趣味 2020-12-11 14:58

I have this as one of my members of the class \'KeyEvent\':

private delegate void eventmethod();

And the constructor:

publi         


        
4条回答
  •  春和景丽
    2020-12-11 15:35

    the thing that you want is called "first class function" http://en.wikipedia.org/wiki/First-class_function

    C#, since version 3, supports anonymous functions and lambda expressions.

    So you can use Func which represents a function taking an argument of type A and returning a value of type R

    from wiki

    static Func MakeDerivative(Func f, double deltaX)
      {
        return (x) => (f(x + deltaX) - f(x - deltaX)) / (2 * deltaX);
      }
    

提交回复
热议问题