6

What the fuck is going on ???

How the "intermediate" c# developers can't do a simple null safe average of even numbers in an array ?!

Why they still write loops and shit (without any nul checks ofc) while it can be done in 1 line :

array?.Where(x => x % 2 == 0).DefaultIfEmpty(0).Average() ?? 0

WHY ? Even Junior c# dev is supposed to know that shit

Comments
  • 4
    Hurray tertiary syntax !

    Oh wait no
    Hurray linq
  • 2
    Doesn't this code treat null entries as zero? It's been a while but aren't null values usually ignored when calculating averages? At least I think that's what happens in SQL.

    1, 4, 5, null, 2 : null ignored, avg = 12/4

    Null treated as zero, avg = 12/5
  • 1
    @nibor the array is specified as int[]. So no nulls possible inside.
  • 0
    @notojavascript so is the DefaultIfEmpty statement redundant?

    Is the null safe part just that the array can be null? It's been too long since I did any c#, but is Where basically an extension method that doesn't throw if the variable is null, so really only the ?? expression is required?
Add Comment