It′s mid-fall which means a new version of C# is coming soon. It′s time to find out what updates will soon appear in the language. Although C#12 has fewer features than previous versions, it still...
I’m sure a lot of work went into this, but it seems crazy that we need another way to initialise collections, and another way to make fixed sized arrays.
This is for custom collections, right? And you don’t even have to use it, you can keep using existing ctors for your custom collections
Worse case scenario you keep doing what we’ve always had to do. But for the 99% of use cases we get a much more streamlined initializer, with extensions to use our own.
they actually have to reference the function by string name.
This is true of a lot of the opt-in language features though, isn’t it? For example, you can just make an .Add method on any IEnumerable type and get collection initializer syntax supported for it, even as an extension method. The same works for Dispose on ref structs I believe, and I remember there being a few other places where this was true (GetAwaiter I think?).
I think for arrays it’s not really a benefit. But for other types it’s superior, compared to the current syntax which just calls add on the list object, which is very inefficient compared to building the whole thing at once which is what this new syntax does.
I’m sure a lot of work went into this, but it seems crazy that we need another way to initialise collections, and another way to make fixed sized arrays.
I just want less boilerplate.
The new list initializing syntax is less boilerplate, no?
Look how clunky it is to add support on a collection. Take the example here:
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/collection-expressions#collection-builder
It involves adding a new builder class, and they actually have to reference the function by string name.
It’s actually making me even more angry now that I look at it in detail.
This is for custom collections, right? And you don’t even have to use it, you can keep using existing ctors for your custom collections
Worse case scenario you keep doing what we’ve always had to do. But for the 99% of use cases we get a much more streamlined initializer, with extensions to use our own.
I don’t see how that’s a bad thing
This is true of a lot of the opt-in language features though, isn’t it? For example, you can just make an
.Add
method on anyIEnumerable
type and get collection initializer syntax supported for it, even as an extension method. The same works forDispose
on ref structs I believe, and I remember there being a few other places where this was true (GetAwaiter
I think?).I think for arrays it’s not really a benefit. But for other types it’s superior, compared to the current syntax which just calls add on the list object, which is very inefficient compared to building the whole thing at once which is what this new syntax does.