I have a delegate that looks like this:
public delegate byte[] CopyPixelOperation(byte[] pixel);
It allows me to inject an arbitrary operation into a function which loops through and modifies each pixel in a bitmap. Here is an example of an implementation of the delegate:
CopyPixelOperation greenify = delegate(byte[] pixel)
{
int redValue = pixel[2];
int greenValue = pixel[1];
int blueValue = pixel[0];
pixel[1] += 10;
pixel[0] -= 10;
pixel[2] -= 10;
return pixel;
};
I’m still a little shaky on lambda expressions and I’m not sure how to reference the individual elements of the array from inside the expression. Is it possible? Does it make sense, or should I just leave this how it is?
delegate()expressions and lambdas aren’t all that different syntactically. At bare minimum it’s a matter of removing thedelegatekeyword and adding a=>operator. You could express your delegate as the following lambda:To further simplify it, you can omit the argument type so
(byte[] pixel)becomespixel, and its type will be inferred from theCopyPixelOperationdelegate type.