C#: IEnumerable, yield return, and lazy evaluation

This page summarizes the projects mentioned and recommended in the original post on news.ycombinator.com

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

    .NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.

  • Unfortunately, while I would love JIT to do such kind of optimizations, this is completely not how it works today.

    First and foremost, '&mut' in Rust is a mutable reference. The C# alternative is just 'ref'. Both point to specific addresses. 'Box' OTOH is a handle for a struct allocated on a heap, just like C#'s one is implementation-wise (except in C# they are further managed by runtime but that's besides the point).

    Now regarding the arrays - 'T[]' is strictly object withouts ifs and buts. There is no "short array" optimisation that would substitute array type with stack-allocated inline buffer. If you want this, you have to write it manually. See: https://github.com/dotnet/runtime/blob/57bfe474518ab5b7cfe6b...

    The last but not least, if the method signature accepts an argument by its interface type e.g. 'HandleEnumerable(IEnumerable values)', then the IEnumerable-implementing struct will be boxed before method can be called. C#/.NET doesn't do full generic monomorphization like Rust. Instead, for class-type generics a common '_Canon' method body will be JITted which will also implicitly accept generic-type arguments to correctly dispatch on an actual of the method. OTOH, for structs, all method calls will be fully monomorphized per each struct type. This means that you can avoid struct boxing when dispatching by interface by rewriting method signature to 'HandleEnumerable(TEnumerable values) where TEnumerable : IEnumerable'. In this case, unless you cast your struct to an interface type explicitly beforehand, the struct will not be boxed.

  • dotnet-nativeaot-labs

    A place to learn about and experiment with .NET NativeAOT on AWS.

  • Speaking of serverless, this might be of interest to you: https://github.com/awslabs/dotnet-nativeaot-labs

  • 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
  • ruby

    The Ruby Programming Language

  • UniTask

    Provides an efficient allocation free async/await integration for Unity.

  • IIRC there's a little more work needed for unity, but Cysharp has you covered (https://github.com/Cysharp/UniTask)

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