开发者

Can and should I turn a delegate that modifies an array of bytes into a Lambda expression?

开发者 https://www.devze.com 2023-04-11 11:53 出处:网络
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

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 t开发者_运维问答his how it is?


Yes, you can use a lambda expression - but personally I probably wouldn't. That looks like a good place to write a method instead:

public static byte[] Greenify(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; 
}

You can easily create the delegate corresponding to that method using a method group conversion when you need it:

CopyPixelOperation operation = Greenify;

or (as a method call argument):

var result = Apply(Greenify).Then(Save); // Or whatever

Unless it's in something like a parallel foreach or a situation where I really need to capture context, I generally prefer to have methods over long anonymous functions.


delegate() expressions and lambdas aren't all that different syntactically. At bare minimum it's a matter of removing the delegate keyword and adding a => operator. You could express your delegate as the following lambda:

CopyPixelOperation greenify = (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; 
};

To further simplify it, you can omit the argument type so (byte[] pixel) becomes pixel, and its type will be inferred from the CopyPixelOperation delegate type.


As far as I know, lambdas and delegates are almost equivalent; the only difference besides syntax is that lambdas can be implicitly typed. It's up to you which is more readable:

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; 
                            };

or

CopyPixelOperation greenify = pixel =>
                            {
                                   int redValue = pixel[2];
                                   int greenValue = pixel[1];
                                   int blueValue = pixel[0];

                                   pixel[1] += 10;
                                   pixel[0] -= 10;
                                   pixel[2] -= 10;

                                   return pixel; 
                            };

Also notice that CopyPixelOperation can be

Func<byte[], byte[]>

(Also notice that your method saves values that it never uses.)

0

精彩评论

暂无评论...
验证码 换一张
取 消