How can I avoid IEnumerable multiple iterations? Is it bad to cast IEnumerable --> ICollection --> IEnumerable to perform validation?

This page summarizes the projects mentioned and recommended in the original post on /r/dotnet

Our great sponsors
  • WorkOS - The modern identity platform for B2B SaaS
  • InfluxDB - Power Real-Time Data Analytics at Scale
  • SaaSHub - Software Alternatives and Reviews
  • AsyncEx

    A helper library for async/await.

  • BTW - My use-case is that I am developing nuget packages. My nuget packages are lower level, similar to this: https://github.com/StephenCleary/AsyncEx

  • silverback

    Silverback is a simple but feature-rich message bus for .NET core (it currently supports Kafka, RabbitMQ and MQTT).

  • I tend to declare the parameter as IReadOnlyCollection because I can safely iterate without extra allocations. I know you could just do a ToList by default but it would mean allocating a few arrays in the process, in worst case. You could be smarter and check if the IEnumerable can be casted to IReadOnlyCollection and perform a ToList only when needed, this would be the most flexible implementation. I sometimes create an extension method for this: https://github.com/BEagle1984/silverback/blob/master/src/Silverback.Core/Util/EnumerableAsCollectionExtensions.cs.

  • WorkOS

    The modern identity platform for B2B SaaS. The APIs are flexible and easy-to-use, supporting authentication, user identity, and complex enterprise features like SSO and SCIM provisioning.

    WorkOS logo
NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts