Collections Just Changed in C# 12 and That’s Good

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody I'm Nick and in this video I'm going to introduce you to a brand new c-sharp 12 feature that aims to make working with collections way way easier the feature is called collection literals and the idea is that because c-sharp is a bit of a painful language to work with when it comes to initializing different collections like dictionaries list arrays or even immutable constructs this feature completely changes how that is done and it makes it very very easy in this video I'm going to show you what it is how it works and why you should really care about it if you like a lot of content and you want to see more make you subscribers more more training check out my courses on domtrain.com so I'm in shoplap.io website that allows me to write some C sharp and I'm using the collection literals Branch so I have the compiler that allows me to use this feature now if you wanted to initialize let's say a list of some type in this case I'm going to use a list of integers you would have something like this you'd have a list of numbers and I can simply say list of numbers dot dump to just print it in the console here now this has been simplified in the past few years for example you no longer have to have the list here you can have just a new like this and this will work however you still need the new keyword you still need these angle brackets and usually what most people tend to do is format it in this specific way you have like three lines of code just to initialize some items and this actually gets more complicated the more items you have lists are not unique in this arrays actually hardly simplification happen to them because they were also very painful to work with for example if you want to have an array with numbers again you'd have something like this back in the day but then this was actually simplified so you no longer need to have the type here you can just do this and this would still work either this also change with this new feature and if you wanted to allocate a span for example so you have a span of integers you would have to do stack unlock then specify the type and then in same fashion you would say something like five six and seven which isn't that nice you have the stacker lock this inter right here it's just a bit much and don't get me started with dictionaries dictionaries along word on its own so if you have like an end of strings and then you want to initialize that you would end up with something like this or dictionary dictionary here yes and this can be simplified by using VAR here or removing this and just using new here because now we can support this in C sharp but it wasn't always the case so quite a bit of stuff to initialize things now collection literals this new feature aims to make this all very very simple with one unified way of dealing with this for example instead of doing this in New Or new list to create the new list of a specific type all you need to do now is I'm gonna get the numbers go here and say square brackets and then paste the numbers and that is it and behind the scenes what's happening is that new list whatever is added but now all you have is these square brackets and that is it does the exact same thing not only that but arrays can also work that way so replace these curly braces with square brackets now I I know that this might look a bit weird because it sort of looks like an array thing that is now used into the context of a list but it gets better or worse depending on who you ask it will also work with stacker lock so you can do this you can remove that stacker lock of the specific type and just have the exact same thing so all three things now will have the same square brackets approach and you might be wondering is this also the case for the dictionary and the truth is it actually is it's a bit different but yes that's the plan it's not supported yet in this Branch but the plan is the following instead of having this syntax you should be able to do this you would say dictionary you would remove that type over here and then you would have the same square bracket so let me just quickly delete that and as you can see this actually compiles as it is right now and then you would have the first value which is one and then its text value which is one and then the second value and then its text value so that will be the exact same thing as this it's not supported as of now and this syntax might change that is the plan or at least one of the planned solution for this now this doesn't end here really because you can do other things with this approach as well if for example you want to create a list out of this list and this span and then maybe inject some other value in the middle you could do the following you can say list of integers is actually and I'm going to call that all numbers equals and I'm going to use the same square brackets and say take this list of numbers as the first parameter then put a 4 in the middle and then take this span of numbers and by the way if you ever had to do this with traditional c-sharp you would know how much of a pain it is to do that now you don't have to worry about it and if you want to dump all the numbers it will concatenate everything into a single list very very easily now I absolutely love this feature and I do have a very reasonable concern which is well what's the right way to do this which is well okay so you have this intera approach which is valid but also this is the exact same thing and the only thing that changes is you go from a curly brace to a square bracket which is interesting because C sharp is getting out of lack for being able to do the same thing multiple times so I think that specific situation it's what's pushing it for me the rest I really like I also am not a big fan of hiding the fact that behind the scenes here you have a stack a lock I like well stacker lock is very visible and you can see it but the benefit of the simplification you get is quite massive and to be honest if you're working with a span you probably know what the stack unlock is so it's a good compromise I think to have something like this this reads very very nicely in my opinion and we did get list patterns already so this should make sense if you are familiar with C sharp 11 already in time now one of the biggest questions you might have is okay what if I have the following what if I have an immutable construct because those immutable constructs are not just a new immutable array for example they have a create method usually for example let's say you have of this immutable array of integers which is created by taking another immutable array and then calling the create method so it doesn't have a traditional Constructor what do you do here well there's actually quite a few ways you can go about this and I think they're actually going to be implemented but the first thing I need to do for this to work or at least feel like it's going to work is add an extra attribute called collection Builder attribute so we're gonna have to use that I'm gonna just put it at the bottom as a helper one and then I'm going to show you what you could reasonably expect for Microsoft to do which is in the proposal as well which there's a link in the description below which is either uses collection Builder attribute to use the type and then from the type points to the method that knows how to create that type of construct so in this case this should be able to work or something along those lines or you can have a constructor-based approach like this one over here where you say collection Builder the type you want to have dot Constructor or dot ctor is the way to point in a string fashion to the Constructor which is here accepting this read-only span of value so those are the two approaches is we might end up seeing when this feature is fully implemented and in C sharp this of course does mean that if you have your own collection type with your own approach you should be able to use that same attribute to point to any create method or Constructor to utilize for your own purposes if you have like a custom list or a customer hasht or any of those other types I think this feature is very cool very simple and it follows the same principle of Microsoft that they're trying to simplify some of the things that are a bit Legacy and I do think the way we initialize collection CC shop Arabic Legacy now you might say this looks a lot like python or and so on but I don't think that's a bad thing especially working with types or numbers but enough with what I think I want to know what you think because as always the reason why I'm bringing this out very very early is so you can see how they're going to work or at least how we expect them to work and then have your thoughts in the comments down below and in The Proposal I'm going to link The Proposal in the description down below you can check it out you can try this you can see if you have any feedback and try to give it to Microsoft to make this even better well that's all I have for you for this video thank you very much for watching and as always keep coding
Info
Channel: Nick Chapsas
Views: 104,541
Rating: undefined out of 5
Keywords: Elfocrash, elfo, coding, .netcore, dot net, core, C#, how to code, tutorial, development, software engineering, microsoft, microsoft mvp, .net core, nick chapsas, chapsas, dotnet, .net, .net 7, .net 8, c# 12, c# 12 new features, C# features, new c# feaures, collection literals, c# collections, c# collection literals, Collections Just Changed in C# 12 and That’s Good
Id: 4w5_AX3ma54
Channel Id: undefined
Length: 8min 0sec (480 seconds)
Published: Thu Jul 13 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.