Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
What's New in C# 13 (learn.microsoft.com)
13 points by algorithmsRcool on May 24, 2024 | hide | past | favorite | 2 comments


The new params collection types is probably the biggest change, this will allow ergonomic but efficient methods to be written that can operate on variable length inputs. This is utilising the Span<T> type which works similarly to a Rust slice.


Yes, it would allow the following lowering strategy:

    // As written
    HandleMultiple(1, 2, 3, 4)

    // As compiled
    var buffer = new InlineArrayInt4().AsSpan();
    buffer[0] = 1;
    buffer[1] = 2;
    buffer[2] = 3;
    buffer[3] = 4;
    HandleMultiple(buffer);
And if the argument buffer is not mutable aka ReadOnlySpan<T>, C# can simply emit a static RVA array embedded into assembly, a constant (provided all arguments are constants too).

Today, you can already do so without the feature with collection expressions e.g. HandleMultiple([1, 2, 3, 4]) but C# 13 will automatically switch existing callsites to the new params span overloads, reducing allocations and improving performance for free.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: