Params Collections - The First NEW C# 13 Feature

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
it's only been a few months since we got C 12 together with net 8 in November 2023 however today I'm already showing you the first C 13 proposal that got implemented in a c 13 preview version in C 12 we got collection Expressions as a new compiler feature as a quick reminder collection Expressions allow us to Define collections with a shorter syntax which reminds me of how we declare arrays in JavaScript and typescript there is no new keyword and we don't use curely braces when using the new syntax all we need is squared brackets and separating the values by a comma collection expressions are great but what new feature do we get with C 13 well the new feature is called PMS Collections and it allows us to use collection types when using the PMS keyword with C 12 and earlier versions We were limited to use arrays when using the PMS keyword for example let's say we want to implement a print names method that accepts a variable number of user names we declare the parameter as an array of string but what if we want to use list or read only span instead of an array with C 12 we are out of luck and the compiler throws an error stating that the params parameter must be a single dimensional array however when I change the branch to the preview feature with C 13 the code compiles and we are able to use list read only span and other collection types when using the pars keyword this takes us to the questions of what advantages does the new C compiler feature provide first of all we need fewer type conversions when using the parms keyword and working with a variable length parameter list consider the following example we first create a list of string next we want to call a method that has a parameter using the par arms keyword without this new compiler feature we call it to ra method or use the spread operator and therefore allocate memory to store the data in a new array when we want the convenient I of a collection type within the method we then create a new instance of that collection type allocating memory again when we use the C 13 params collections combined with the c 12 collection Expressions we don't allocate memory multiple times we don't create a short lift array instead we can provide the instance that we created of any collection type and provide it as the argument for for a method that uses the params keyword besides the performance aspects we also improve the general code quality because we can write shorter and cleaner code when using the params keyword in our code the reason is that we get around the previously existing limitation of only being able to declare arrays when using the params keyword so what's the impact of parm's collections in C 13 for my everyday work work I'm mostly writing business applications and I cannot remember when I last used the params keyword to declare a method I usually declare and use collection types when providing multiple arguments between different methods however I know that Library authors often have to overload methods and provide different apis for single and multiple arguments of the same type and therefore I can see a lot of potential for code Improvement and less method overloading when implementing those libraries in my opinion it's more about the quality of life Improvement than about performance improvements now what do you think how much impact will this new feature have on your code in conclusion adding params collections is continuing the journey that was started in C 12 where we got collection Expressions they allow us to write simpler apis and could be a performance boost in certain areas for me it's most likely a modernization of a keyword that has been in C for a very long time par arm's collections will close the gap that has been opened by improving the language over many years will it completely change the way I write my C code in the future probably not however will it be handy in some situations absolutely if you're interested in more details you can find a link in the video description to the C language feature proposal written by the C language design team keep in mind that parm's collections are still in preview and that you can provide feedback also they could change the feature at any time or completely remove it from C 13 only time will tell if you want to learn more about net development consider subscribing to the channel Channel and I will see you in the next video
Info
Channel: Claudio Bernasconi
Views: 899
Rating: undefined out of 5
Keywords: C#, C# 13, CSharp, CSharp 13, Params Collections, Collection Expressions, C# 13 Preview, Feature Preview, .NET, .NET 9, dotnet, dotnet 9, Claudio Bernasconi, .NET Core
Id: MgpM0LDL8d4
Channel Id: undefined
Length: 5min 33sec (333 seconds)
Published: Thu Jan 25 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.