2 ms·
Java’s type erasure means that generic type information is not available at runtime. C# lambdas: although non-capturing lambdas do not allocate, capturing lamb
by nuancebydefault 2y ago
Java’s type erasure means that generic type information is not available at runtime.
C# lambdas: although non-capturing lambdas do not allocate, capturing lambdas do.
"calls through them are virtual" is due to the underlying implementation of delegates in .NET.
- neonsunset 2y agoWell, yes, but delegates as a term is not often used in other languages so I did not mention them for simplicity's sake. For what it's worth - the real issue in C# is not even the virtual calls but the way Roslyn caches lazily allocated non-capturing lambda instances. It does so in a compiler-unfriendly way due to questionable design decisions inside Roslyn. Luckily, this has a high chance of changing in .NET 10. Ideally, by the time it releases hopefully the compiler will both understand the Roslyn's pattern of caching better and be able to stack-allocate non-escaping lambda closure instances. Lambdas capturing 'this' inside instance methods of the object they refer to do not allocate either.