Stop using LINQ to order your primitive collections in C#

Check out my courses:
Become a Patreon and get source code access:

Hello everybody I’m Nick and in this video I will show you a couple of new LINQ methods introduced in .NET 7 that you probably shouldn’t be using. There are some even faster and more memory efficient alternatives that you can use instead, depending on your usecase.

Don’t forget to comment, like and subscribe 🙂

Social Media:
Follow me on GitHub:
Follow me on Twitter:
Connect on LinkedIn:

Keep coding merch:

#csharp #dotnet


  1. Great video Nick! I was really hoping that you would run the benchmarks with the Params attribute to see what happens with 1_000, 10_000 and 1_000_000 sized collections. I would also really like to see the results from the sort using spans method. Really great video though as always! Keep them coming…

  2. I believe that a static lambda like x => x with no closure will not allocate anything. It's compiled as a static method in the using class scope. It can be seen by decompiling the output managed DLL. Only delegates and lambdas that actually capture some state will be compiled to a separate class that then needs to be instantiated (which causes the allocation). No closure delegates will have their object set to null.

  3. Do linq methods perform sort immediately, or can it be deferred or halted. Like say we order and take 100 out of a million, will the performance be the same as we array sort said million. I would benchmark it later, now I am from phone. But if someone beats me to it, you are welcome.

  4. It seems like it would be a simple optimisation to have order call the underlying sort functions. I guess the extension method is on the interface, but it seems like it would be worth it to check the type of the collection.

  5. This comment is unrelated to this video. Hey Nick, I really hate ENUMs. Specifically when newer devs use them in service contracts and change somewhat regularly. Would you mind putting together a video on a more elegant way to handle this topic that doesn't break contacts? I see this specifically in an EntityType or EventType and the moment a new one is added everything breaks.

  6. Maybe worth pointing out by having three different arrays of different random numbers it can make for inconsistent results

  7. This might sound like a stupid question, but can any body tell me what IDE Nick is using? I'm currently using Visual Studio 2019. I like using it but Nick's seems more responsive than mine.

  8. Theos! You are my defacto no.1 resource for C# on YouTube. Enjoying the flow and straight to the point vids. I tip my hat for you sir 🙂

  9. Order/OrderBy advantages:
    + purity (i.e. doesn't change underlying collection)
    + simplicity
    + readability
    + same for primitive/complex types
    + extension method of IEnumerable<T> => you don't have to have a collection before and ToArray/ToList can be way down the chain of other methods
    + expression tree (allows for other optimizations etc.)

    Sort advantages:
    + faster (not always)
    + more memory efficient

    So it depends. I believe Order/OrderBy is better choice in most of the cases. Title of the video is a bit misleading.

  10. to be clear – when you compare sorting methods you should compare this same data sets for each algorithm so instead of creating new collections of random items each time you should create this collection once and work on a copy of this collection

  11. What aout sorting objects using a property inside them, I think that's the most important use case.
    Can we use List.Sort with IComparison In that case?

  12. This video feels like 12 minutes just to get talked down to and find out something I already knew and would have no problem finding on google if I didn’t…

  13. If you like ghelbrubghluru content, and you wanna see more, make sure you subscribe and ring the scrubhshdsdfshsddhaf notification bell ……..

Leave a Reply

© 2023 53GB