c# Make the function a function parameter



  • There are many functions like flipImageV. Before and after the code, these functions shall have the same lines of the code. Whatever's in every flipImageV function, I want to do one where I'm going to transfer the right function. ♪ ♪

    This is a universal function to which I hand over a selected type flipImageV function:

    protected void editImage(delegate func) {
            currentImage.ImageUrl = @"..\images\img0.jpg";
            func();
            photo.Save(@"~\" + imagePath);
        }
    

    Type flipImageV function:

    protected void flipImageV(object sender, EventArgs e) {
            photo.FlipVertical();
        }
    

    Use of that template:

    editImage(flipImageV);
    

    or

    protected void flipImageV(object sender, EventArgs e) {
            editImage(photo.FlipVertical());
        }
    

    What should be instead delegate func? How do we do that?



  • Just use it. Action:

    protected void editImage(Action func) {
        currentImage.ImageUrl = @"..\images\img0.jpg";
        func();
        photo.Save(@"~\" + imagePath);
    }
    

    Call it that way:

    editImage(() => photo.FlipVertical());
    

    If you need to install a function like that. flipImageVwhich receives two ignored arguments:

    editImage(() => flipImageV(null, EventArgs.Empty));
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2