Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format lambda expressions and anonymous methods for maximum readibility?

Tags:

c#

.net

lambda

For instance:

Sorter.SortBy ( array, ( a, b ) =>
{
    return a > b;
} );

What's the best way to format them for maximum readibility?

Also think of it for one parameter lambda version, and other cases that might be used commonly.

What are the guidelines?

like image 828
Joan Venge Avatar asked Jul 01 '26 18:07

Joan Venge


1 Answers

Sorter.Filter(array, a => a.IsOK);

Sorter.SortBy(array, (a, b) => a > b);

Collection.Apply(array, (a)       => a * a, // i like lining things up
                        (x, y, z) => WhipIt(x, y) / z,
                        (a, b)    => a + b);

Evaluator.Confuse(array, (a, func) =>  // this is a big one, engage curly braces
{
    if(a.Flag) return 0;
    else
    {
        var x = func(a);
        if(x < 0) return -1;
        else return x * 2;
    }
});
like image 91
mqp Avatar answered Jul 03 '26 07:07

mqp