←back to thread

201 points olvy0 | 1 comments | | HN request time: 0.203s | source
Show context
high_na_euv ◴[] No.41878416[source]
LINQ is so fucking useful and well designed feature of .NET ecosystem that it is unreal when you gotta use lang which doesnt have such a thing.

C# design team is/was unparalleled

replies(7): >>41878459 #>>41878543 #>>41878588 #>>41878686 #>>41879163 #>>41879194 #>>41879315 #
hggigg ◴[] No.41878686[source]
LINQ is a veritable footgun in any large team I find. While it's extremely powerful and really nice, it's so so so easy to blow your toes off if you don't know what you are doing. Some of my favourite screw ups I saw:

* Not understanding when something is evaluated.

* Not understanding the computational complexity of multiple chained operations/aggregates.

* Not understanding the expectation that Single() requires exactly one of something.

* Not understanding how damn hard it is to test LINQ stuff.

replies(5): >>41878737 #>>41878748 #>>41878838 #>>41879379 #>>41879440 #
1. jve ◴[] No.41879440[source]
Single gives you some guarantees about the returned value. Use First/FirstOrDefault if you don't need those guarantees. You can also provide predicate for FirstOrDefault to select First element that matches your predicate.

> Enumerable.Single Method - Returns a single, specific element of a sequence.

Some overload descriptions:

- Returns the only element of a sequence that satisfies a specified condition, and throws an exception if more than one such element exists.

- Returns the only element of a sequence, and throws an exception if there is not exactly one element in the sequence.

https://learn.microsoft.com/en-us/dotnet/api/system.linq.enu...

> Enumerable.FirstOrDefault Method - Returns the first element of a sequence, or a default value if no element is found.

https://learn.microsoft.com/en-us/dotnet/api/system.linq.enu...