Тёмный

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

Nick Chapsas
Подписаться 302 тыс.
Просмотров 105 тыс.
50% 1

Check out my courses: dometrain.com
Become a Patreon and get source code access: / nickchapsas
Hello everybody, I'm Nick, and in this video, I will show a new feature coming in C# 12 called Collection Literals. Collection literals aim to simplify how we are working with collections such as Lists, Spans, Dictionaries and Immutable types in C#, but it kinda looks like we are copying Python.
Proposal: github.com/dot...
Workshops: bit.ly/nickwor...
Don't forget to comment, like and subscribe :)
Social Media:
Follow me on GitHub: bit.ly/ChapsasG...
Follow me on Twitter: bit.ly/ChapsasT...
Connect on LinkedIn: bit.ly/ChapsasL...
Keep coding merch: keepcoding.shop
#csharp #dotnet

Опубликовано:

 

6 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 325   
@Palladin007
@Palladin007 Год назад
The code with the span does not use a stackalloc. It is an array as span. SharpLab can show you
@nickchapsas
@nickchapsas Год назад
You're right, I totally missed that! Pinning this so people can see it.
@Krimog
@Krimog Год назад
Maybe the should add a Span s = stackalloc [1, 2, 3]; feature?
@Krimog
@Krimog Год назад
@@Khitiara_ The idea behind Collection Literals is to simplify it and make it consistent.
@Khitiara_
@Khitiara_ Год назад
@@Krimog true, and Im hoping they add stackalloc support to those - its been a topic in the proposal documents for a while now
@Khitiara_
@Khitiara_ Год назад
@@Krimog the current state of the proposal seems to be that the compiler may switch to stackalloc later for performance and the design wont preclude that but i guess it isnt implemented yet
@PaulSinnema
@PaulSinnema Год назад
I've been a C# programmer for more than 20 years but cannot keep up without your insights. I'm retired now but still love programming. Thank you Nick for keeping me up to date.
@Dojan5
@Dojan5 Год назад
Ooh! The collection spreading is one of my favourite aspects of JS. It's just so easy and intuitive to work with. Love to see it in C# too!
@Vim_Tim
@Vim_Tim Год назад
JS spread operator is also a common performance bottleneck, so use responsibly :)
@fifty-plus
@fifty-plus Год назад
Indeed, I'm interested to dig deeper into this and see if it follows array semantics instead of iterator semantics like JS does - which far too commonly and easily hides performance issues.
@creamyhorror
@creamyhorror Год назад
Now if C# could somehow implement JS-style destructuring operators and get really dynamic with object properties...lol, I'd love to see "Dynamic C#".
@christopherwilliams3293
@christopherwilliams3293 9 месяцев назад
Hopefully, we can clean things up some more and create an object with {} and use .. to copy things to it. This application will be very useful when working with JSON.
@marna_li
@marna_li Год назад
Nice improvement. I would rather use curly braces { key: value, ... } for the dictionary syntax as that mirrors what is used in pattern matching as well as other languages.
@Z3rgatul
@Z3rgatul Год назад
I think there can be language parser issues with this approach, and that's why they are trying to find something else
@ElCerdoBlanco
@ElCerdoBlanco Год назад
This would also resemble the JSON syntax, which I think is the basis for most languages, that do it the same way.
@CharlesBurnsPrime
@CharlesBurnsPrime Год назад
This is the last remaining Python convenience that I miss in C# (the other big one being a better array syntax, which was implemented some time ago and used in this video). I am so impressed with the C# team in recent years. I haven't even considered moving to Kotlin or another language. I hope it will be possible to initialize immutable types in the same way, even if there's a tiny bit of magic under the hood.
@andy010
@andy010 Год назад
I would just use {} instead of [], as they are already implemented and just drop the need for the new keyword on lists and such. For the span, the type could also be implicitly derived, but I'd keep the stackalloc
@harrytsang1501
@harrytsang1501 Год назад
Yeah, and I don’t like using the same symbol for array and dictionaries. Why not just keep the {} and infer the type/new/alloc part
@Vastlee
@Vastlee Год назад
I do love the feature, but I'm wondering why they couldn't maintain the same syntax they do as the array? For instance why couldn't we just have List nums = { 1, 2, 3 }; The compiler knows it's a collection by the type definition.
@thinker2273
@thinker2273 11 месяцев назад
In case the team ever wants to do block expressions, List nums = { 1 }; could be ambiguous.
@klocugh12
@klocugh12 Год назад
I really hope this makes it into official version.
@OllyWood688
@OllyWood688 Год назад
6:14 _"To be honest, if you work with a span, you probably know what a stackalloc is"_ This is the kind of bold assumption that will ruin your weekend with a butterfly effect breaking change on a friday afternoon in an otherwise unrelated part of the solution.
@temp50
@temp50 Год назад
xD Yeah, a couple of years later nobody will infer that in mind :D
@billy65bob
@billy65bob Год назад
I would've preferred if the syntax used {}'s instead of []'s for consistency with the other collection initialisers. I think I'd also prefer the special 'Dictionary' syntax to use '=' instead, so it's consistent with both anonymous class objects, and object initialisers. EDIT: Or if you want to use : for dictionaries, then allow me to do var = raw JSON Object and get a dictionary set up that way.
@marna_li
@marna_li Год назад
Sure an array is an array. That syntax is established. A collection is a sequence and the sure we can adopt the [1, 2, 3] syntax for that . A dictionary would be its own thing. I would like the Dictionary to use curly braces: { "key": 42 } as it mirrors anonymous objects and object pattern syntax. Which I also would add support for: object-like pattern syntax on dictionaries.
@maskettaman1488
@maskettaman1488 Год назад
Not sure why we need more and more syntax options to do the same things we've already been doing without issue. Sure this is convenient and I'll probably use it but is it really necessary? There was no better use of time for the C# team? Great video as always Nick
@ciberman
@ciberman Год назад
I almost cried of joy when I saw the intro
@janbodnar7815
@janbodnar7815 Год назад
This is really great! C# already has a decent syntax for collection initializers. (Except for Dictionary instead of Map). C# has roots in Java. (Remember the ArrayList thing?) It has improved its syntax a lot. And if this gets through, this would be another great improvement. Also, this simple syntax is used by many other languages, including JS, Haskell, Groovy, F#.
@jameseze9469
@jameseze9469 Год назад
The only thing that gets me angry is the fact that the guy's working on C# knows all the easy/elegant way to write codes, and they've seen it on other languages, but they just beat around the bush for years, then come back and say "well, we just gotta do it". Meanwhile, they've already created more ways to do one thing. One day, they'll deprecate List mylist = new(){1,2,3}. Then later bring it back as a different function.
@der.Schtefan
@der.Schtefan Год назад
Using [ ] as syntax is what they should have done in the first place.
@MyriadColorsCM
@MyriadColorsCM Год назад
Honestly, this will be such a huge help in making C# code more readable. Its a great feature.
@smuddy
@smuddy Год назад
As a typescript/C# dev i really appreciate these changes.
@ryan-heath
@ryan-heath Год назад
Although the ranges look a bit weird, I really like the seamless "listify" of a variable! int [] i = [1,2,3,4]; int [] j = [5,6,7,8]; int a = 123; List k = [ ..i[1..2], a, ..j[3..] ]; var str = "hello"; UseList([str]); void UseList(List values) { }
@bsoter
@bsoter Год назад
Looks a bit like F# syntax for collection initializers but simplified. In F# it'd be [1; 2; 3] for a list, [|1; 2; 3|] for an array and [(1, "a"); (2, "b"); (3, "c")] for dictionary. Tbh I dunno which one I prefer. Same syntax for all collection types is easier to remember (I'm always stuck for a second when initializing list in F# - it begins with '[' or '[|'...) and there's no problem when applying it to custom collections. The obvious pro for a distinctive syntax is it could be used with 'var' keyword because the syntax itself gives us full info about collection type.
@escribe6751
@escribe6751 Год назад
It would be nice to have a more consistent approach to this, why not use curly brackets for this? It is already used to initialise data for collections. This would make it imo easier to understand and easier to use. Square brackets for accessing data, curly brackets for initializing data. Keep it consistent. I guess an argument dor square brackets would be that they are also used in string slices (or whatever they are called), and they have a similar syntax to the appendeding initialisation of collections..
@recursiv
@recursiv Год назад
This is discussed (extensively) on the proposal comments section. TL;DR a lot of people thought of curly braces, but it's not happening. For specific reasons.
@Blu3Souls
@Blu3Souls Год назад
I wish there were channels like yours for other languages like Rust. There are so many videos about "the basics" but not enough about more advanced or new concepts and how to actually use them in a real project.
@alfany6252
@alfany6252 Год назад
FINALLY!! Even F# has it's own literals for different collections.
@r-naotwo6290
@r-naotwo6290 Год назад
I love it and I hope the proposed one for the dictionary is maintained. Thanks!
@CarrotCakeMake
@CarrotCakeMake Год назад
Or Rust, let array = [1, 2, 3]; let mut vec = vec![1, 2, 3]; let mut tree : BTreeSet = [1, 2, 3].into_iter().collect(); And this works with user defined collections.
@Herio7
@Herio7 Год назад
More concise code is always welcome however current approach is ok in variable initialization. Variable declaration line being 10-15 character shorter is lesser deal than in more "buried" places. What I'm more interested is how clever compiler will be when it comes to more "buried" parts linq chains, short lambdas or inline created method parameters, especially when IEnumerable is expected. These places would benefit the most from the shorter code. Especially when you just want to have "temporary" IEnumerable made from very few elements or just wrapping a single one. I hope constructs like Enumerable.SelectMany(x => [x, DoWithX(x)]) or foreach(var x in [1,2,3]) will be implemented because right now it requires casting which basically is reshuffling of current initialization.
@fr3ddyfr3sh
@fr3ddyfr3sh Год назад
Finally! Learned programming with Python, worked with c# for 10 years. so happy to see this.
@aoisonne1461
@aoisonne1461 Год назад
I'd rather if they keep it as List list = { 1, 2, 3 } rather than List list = [ 1, 2, 3 ]. I'm so used to using { } for initializing throughout the past decade that using [ ] feels really weird to me. For most cases, it's as simple as just removing the new keyword, so why not just do that?
@cypher9000
@cypher9000 Год назад
I'd actually say C# is taking features from JavaScript, or probably in this case TypeScript because Anders Hejlsberg is responsible for both. As for other things I'd prefer them to use curly braces for Dictionary declarations - { key: value } - like they do in JS. And to instantiate immutable collections it would be nice if you could just do ImmutableArray = [ 1, 2, 3 ]; without any method calls.
@owensigurdson8610
@owensigurdson8610 Год назад
They shouldn't bother with arrays and List. Dictionaries could borrow the Python approach however: var dict = new Dictionary { 1 : "a", 2 : "b"}. Honestly, this is all I would do. C# can borrow ideas from other languages but not at the expense of getting overly weird.
@nomadshiba
@nomadshiba Год назад
seeing how c# evolved, and seeing what you can do with typescript types, and how rust manages heap memory, makes me wanna write my own language taking inspartions from all of them.
@Yous0147
@Yous0147 Год назад
This is awesome, I'm super happy to see it coming in the future. I'm surprised they went with square [...] instead of curly {...} brackets, I'd have assumed they'd keep the overall "physiology" of list and dictionary generation follow each other such that it stays intuitive even for a newcomer. I can only imagine it might be a parsing issue or perhaps to clearly indicate it isn't a constructor method but a shorthand.
@StarfoxHUN
@StarfoxHUN Год назад
As long as it does not cause any kind of confusion(like what happens with how heavily c# began to override '?' doing multiple different tasks), i think there's no such thing as 'bad' feature. And apart from what you said with the Span, i see no confusion here. I think one who never heard of this feature or even have limited c# knowledge looks at the code knows by default what it does and what will happen does not get confused by it.
@Subjective0
@Subjective0 Год назад
I couldn't stop laughing for joy. It might be a small thing, but damn, it will be a nice feature to finally be added.
@The12thAvenger
@The12thAvenger Год назад
So they removed the need for new() (not necessarily good because it made even you believe that the Span was using stackalloc when it wasn't) and just added syntax from another language which is inconsistent with c# syntax... Since arrays already have this feature, uniform initialization between collections wouldn't be bad but using [ ] instead of { } seems utterly unnecessary, as does the syntax for dictionaries.
@viktorstojanovic9007
@viktorstojanovic9007 Год назад
Why didnt they just use {} for everything not just arrays
@omriliad659
@omriliad659 Год назад
I would just think of all of these as an implicit conversion from array. You can easily understand converting array to list and back, so converting them on compile time (or even earlier) still feels fine for me. I would even go as far as saying that a dictionary is also easy to convert from a list (or array) of key-value pairs, so the language can also have it as implicit conversion. As long as there's exactly one way to understand a syntax, and it doesn't hide anything important (such as runtime loops, scope, heap/stack etc), I think it's good for the language to understand (unless it bloats the language, which is not the case here)
@nanvlad
@nanvlad Год назад
looks fancy, good approach. For me all I need is a shortcut to initialize array property with an empty collection. Like int[] ArrayProperty { get; set; } = []; instead onf in[] ArrayProperty { get; set; } = Array.Empty(); But this may cause a confusion between empty array reference and new array initialization.
@Kitulous
@Kitulous Год назад
I'd special case empty immutable (not-addable) collections and intern them
@redradist
@redradist Год назад
This feature could be done more in C# syntax, instead of [] to use {}: List array = {1, 2, 4, 5}; Just like it is done in C++
@MichaelBattaglia
@MichaelBattaglia Год назад
I don't mind pulling features from other languages. As long as C# doesn't make white space characters as Python!
@klaxxon__
@klaxxon__ 11 месяцев назад
This looks amazing, and even better if/when they eventually introduce the "default" inferred collection type (when you assign an unspecified collection into var). I would be perfectly happy if they started obsoleting some of the older forms of syntax with the option to turn them back on using compiler switches.
@VaragornX
@VaragornX Год назад
Yeah, too many ways to do the exact same thing. Atm I feel like getting diabetes from all the syntactic sugar in C#.
@diadetediotedio6918
@diadetediotedio6918 Год назад
Just program in C bro
@dennycrane2938
@dennycrane2938 Год назад
These types of things or syntactical sugars add up pretty quick in terms of productivity. Whenever I'm in python I wish C# had some of these features. With javascript, the spread operator (with objects) is another one which I have no clue how that could even work in C# but I can wish.
@sealkeen
@sealkeen Год назад
It is not a paint in the ace to concatenate an array in fact because of the LINQ queries and AddRange() method in List < > which is even more readable.
@mohan7273
@mohan7273 Год назад
This looks really good but I was wondering what will happen if we do var x = [1,2,3,4] . Will it take it as a list or a array or do we get a compilation error?
@dmytroyehorov2905
@dmytroyehorov2905 Год назад
In F# this already implemented. Square brackets for lists, square with | for arrays.
@VoroninPavel
@VoroninPavel Год назад
yet no literal for the dictionary/map
@oscareriksson9414
@oscareriksson9414 Год назад
Yes this is good. Usually syntax sugers are kind of "meh" for me. But this is very good. It is a pain sometimes when initializing arrays and array abstractions such as list, dictionary etc. Also I like that C# is moving away from that old OOP stuff and is also incorporating concepts from other styles. I just hope C# will not become all in functional instead of OOP or what ever, but ever more flexible. Also have you got any videos that shows examples of configuring the GC? If I understand correctly, you can actually replace GC with other GC and there are many settings too
@antonmartyniuk
@antonmartyniuk Год назад
It looks like JavaScript or TypeScript and I absolutely love it
@benesste
@benesste Год назад
I did not know that the List does have a Dump method.
@nickchapsas
@nickchapsas Год назад
It doesn’t. It’s a sharplab thing
@RobinHood70
@RobinHood70 Год назад
I was about to say the same thing, but with Nick's reply, I think "Dammit!" is more appropriate. 😛
@petrzurek5713
@petrzurek5713 Год назад
Shame there is no proposal for this feature. Or is there????
@T33K3SS3LCH3N
@T33K3SS3LCH3N Год назад
C: I am basically every programming language because of my macros C#: I'm literally every programming language.
@zbaktube
@zbaktube Год назад
I believe c# is obsolete. But not the expressiveness of the language itself. Take a look on async-await. If in a chain of function calling (the fancy name with the graph does not pop into my mind right now) you have an async, then it is advised to change all other functions to async. As a backend dev, I either save something (async) or I am calling a webservice (async). So, I need to write thousand times a day: async-await. Does not make sense, especially because this can be automatically decided on the fly. I would love to see a c# where I can configure how my code behaves, and choose auto async-await functionality, or different memory handling ,etc. And leave the language distilled, with a theoretically solid background. How do you find design patterns regarding to new features in c#? Do you write them differently now? The language turns into more procedural/functional. Should we reconsider using anemic models? what will be the desired style? Following guts feelings and forget the theory c# was built on? These feature-stealing looks cool, but I feel c# looses its solid ground. If someone wants to code python-style, do it in python. same with go. But, doing python or go in c# ....There are so many other ways to enhance c#, I feel a bit of wasting time to force it into this direction. What do you think?
@UrzaRage778
@UrzaRage778 Год назад
This is unironically the best feature they could have made. This is one of the things I love about Python, and the fact that it's coming to the C# world? Huge win!
@mykhailokozlov6641
@mykhailokozlov6641 Год назад
I wish the next "big" thing for C# would be union types someday. When switching from typescript back and forth I really miss this feature in C#.
@z0nx
@z0nx Год назад
OneOf for unions, inheritance w/ClosedTypeHierarchyDiagnosticSuppressor for discriminated unions. Don't mind me just puking in the corner
@smallfox8623
@smallfox8623 Год назад
There is at least one proposal for type unions so I imagine it will happen one day relatively soon
@z0nx
@z0nx Год назад
@@smallfox8623 The soon for DUs has easily been passed by now imo
@Felipe-mg1pw
@Felipe-mg1pw Год назад
I would love union types. So, you don't always have to create multiple fields where only one of them is used afterwards
@NoName-1337
@NoName-1337 Год назад
As also a TS dev, I appreciate this feature.
@DarraghJones
@DarraghJones Год назад
there's also this confusing syntax which uses {} and looks like an assignment, but is actually calling Add under the hood. So maybe using [] is more appropriate to indicate you're initialising a collection, and not adding to one. var n = new Numbers { List = { 4, 5, 6 } }; Console.WriteLine(string.Join(",", n.List)); public class Numbers { public List List { get; } = new() { 1, 2, 3 }; }
@smallfox8623
@smallfox8623 Год назад
The new syntax also calls Add under the hood for every element. These two lines are exactly equivalent in IL and both call Add for each of the three elements. List list1 = [10, 12, 20]; List list1 = new() {10, 12, 20};
@DarraghJones
@DarraghJones Год назад
@@smallfox8623 ok, fair enough. ignore me so :)
@MrAbrazildo
@MrAbrazildo Год назад
1:55, do the 3 lines are mandatory?! In C++: std::list listOfNumbers { 1, 2, 3 }; std::span spanOfNumbers { 1, 2, 3 }; std::map dictionary { { 1, "one" }, { 2, "two" } };
@hemant-sathe
@hemant-sathe Год назад
In the interest of showing distinction with the arrays, using curly braces for modifiable collections (those with add, remove methods as example) and using sq brackets for arrays and similar objects would be more appropriate and consistent.
@dcuccia
@dcuccia Год назад
I see your point but disagree, as it would diverge from list pattern matching syntax
@parlor3115
@parlor3115 Год назад
The syntax doesn't look nice nor fits with other C# code. The dictionay should always be with curlies imo (though they probably opted for the brancket because of some compiler limitation). And most of the overhead is still there having to specify the type of the variable.
@alexandergoncharov9610
@alexandergoncharov9610 Год назад
Finally a real comment without the wow. The syntax looks ugly and unreadable.
@dhochee
@dhochee Год назад
@@alexandergoncharov9610 I think you meant "Finally a comment with the same opinion as me". C# is my favorite language but I regularly work with JavaScript and Python and I have long been annoyed by the unnecessarily verbose syntax for list initializers in C#. That you find it less readable does not mean that most developers will. Even though I agree with some of the criticisms, I think it's a great improvement and you'll get used to it too.
@alexandergoncharov9610
@alexandergoncharov9610 Год назад
​@@dhochee I don't believe it when the crowd screams wow. By the way, Nick has a video, for which great respect, where he shows one very wow way DI (dependency injection) for c# net core, which the minority immediately criticized for unclear behavoir. The same var keyword, is a very harmful thing. But of course this is only my wrong opinion. Have a nice day :)
@salameez
@salameez Год назад
Really wish it would be smart enough to understand something like List strings = [1, 2, 3].Select(i => i.ToString()).ToList();
@killymxi
@killymxi Год назад
Seems like the curly brackets shorthand for arrays was not forward thinking enough and is one of its kind. How it sneaked into the language like that, considering there are other features delayed forever until they decide on universal solutions... So, I'd deprecate that one and always use the new universal syntax. My argument pro square brackets syntax: It makes collections initialization distinct from all other code blocks. Wrapping curly braced objects into parentheses due to ambiguity is kinda annoying. If this allows to avoid that inconvenience when returning an anonymous collection instance, then it is a wise choice.
@diadetediotedio6918
@diadetediotedio6918 Год назад
I wish I had a syntax closer to what we already have than the one with '[]'. Why not just use the '{}'? Even if we keep this syntax, for spans I think a syntax like this would be more appropriated: stackallock [1, 2, 3] Since we could then use var
@Kitulous
@Kitulous Год назад
Span x = [ 1, 2, 3 ]; is shorter than var x = stackalloc [ 1, 2, 3 ]; and span indicates that this is on a stack
@diadetediotedio6918
@diadetediotedio6918 Год назад
@@Kitulous I don't know if your answer was sarcastic, but it doesn't indicate so. Span's can be on the heap too, since you can assign an array to a span, or even an unmanaged memory pointer. The "Span" syntax would also be extremely ambiguous considering that Span is a generic type in C#, stackalloc complies with the already existing syntax of: Span x = stackalloc int[32]; And it allows for greater mobility in use, so it seems to be an objectively better option than your idea.
@MiSt3300
@MiSt3300 Год назад
Im very glad of these changes, it's much more readable, it looks more like typescript
@EudaderurScheiss
@EudaderurScheiss Год назад
i love that c# is like, lets steal all the good stuff
@betterlifeexe4378
@betterlifeexe4378 Год назад
Simplicity is great for readability so long as it doesn't come at the cost of something else. like you might loose specificity. Luckily these collections are still labeled both by name and type so this simplification seems only a good thing.
@Crozz22
@Crozz22 Год назад
My immediate question; is the new syntax converted at compile-time to the equivalent old syntax? Or does it initialize an intermediary collection and thus have a performance hit (however small)
@ryan-heath
@ryan-heath Год назад
var i = new int[] { 1, 2, 3, 4 }; int[] j = [1,2,3,4]; in the current version, i and j point to the same memory location. So initialization is exactly the same.
@CreativeB34ST
@CreativeB34ST Год назад
Would have liked if they had a different syntax for arrays, lists and spans(square brackets for one, curly braces for the other, and then even something else), so you could still use it with the var keyword. If you use the var keyword with this new feature, C# probably doesn't know if you want a list, an array or a span. Also wished the 2 dots were 3 dots, so it becomes the same syntax as javascript's destructuring
@phizc
@phizc Год назад
The [ 1 : "One ] syntax is unique to Dictionary, so the compiler should be capable of determining var as Dictionary. Not sure if it's planned, but it should be possible.
@GabrielGonzalez2
@GabrielGonzalez2 Год назад
I'm curious why they went with square brackets instead of pulling from C++ with its curly brackets which are used in the exact same way (just like the array syntax). I wonder if there was some sort of ambiguity with some other feature i can't think of?
@Maskedmous
@Maskedmous Год назад
I find the current way just fine as it is. But I use var to not need to specify the type twice, along with other reasons to use var. It doesn't work with var as there is no type declaration. I won't be able to utilize this feature, but I'm not bothered either. For the ppl that like it strong typed instead of var, I can see why you'd want this feature to write less at the right hand side. The left hand side is already clear enough what the type should be and the right hand side will be easily constructed this way.
@hosseinghahari3557
@hosseinghahari3557 Год назад
I am so happy that I've chosen c# as my first programming language to learn , it's so powerful and great features and amazing support from Microsoft and great future
@nthonymiller
@nthonymiller Год назад
Love it, just like javascript/typescript. I wish the spread was three dots not two.
@lordicemaniac
@lordicemaniac Год назад
i love it, i had exacly same problem with having both {} and [] doing the same for array, but i get why they used [], because of those operators ".." (not sure if there are more operators available for [])
@MirrorsEdgeGamer01
@MirrorsEdgeGamer01 Год назад
It’s looking similar to how Swift initialises dictionaries and arrays.
@antosha2224
@antosha2224 Год назад
Omg finally c# designers add useful simple way to creating collection objects.
@saeedbarari2207
@saeedbarari2207 Год назад
creating a list was never a big deal for me. the feature is cool, but let's be honest, it's just modern syntax sugar and will make code styles even less consistent
@onyxgc2568
@onyxgc2568 Год назад
Yeah totally agree with you, its only hard for non c# devs. c# used fo have a consistent syntax now c# is becoming javascript 😂
@saeedbarari2207
@saeedbarari2207 Год назад
@@onyxgc2568 and that's concerning. that small productivity boost might not be worth having to learn extra syntax.
@the-avid-engineer
@the-avid-engineer Год назад
I think this is great, but the square brackets for dictionaries feels odd because many popular languages use curly brackets
@dennycrane2938
@dennycrane2938 Год назад
wym? 've been using square brackets in dictionary initializers for a long time now. Because `{ [Foo] = Bar }` reads better than { { Foo, Bar } } in longer lists imo
@the-avid-engineer
@the-avid-engineer Год назад
@@dennycrane2938 What you’re saying has nothing to do with what I’m saying. Those square brackets surround the key, not the whole dictionary. (Also, `{["key"]: "value"}` is valid JavaScript)
@matthias916
@matthias916 Год назад
I love how C# is adding more and more functional syntactical sugar
@no-bc4kc
@no-bc4kc Год назад
This looks really interesting and I for one am looking forward to using it
@emerynoel567
@emerynoel567 Год назад
I like it. Very similar to JS, at least in syntax. Love the spread-style operator, too.
@ricardodegenova
@ricardodegenova Год назад
Wow! I really like these changes coming to CSharp! I think those are good features overall and don't really dilute CSharp at all.
@xavier.xiques
@xavier.xiques Год назад
I was waiting for this a long time 👏
@thedreadedgman
@thedreadedgman Год назад
I don't hate it... the only thing that bothers me is that it hides the new... I like to be able to search for "new " to know where my code is allocating new objects - for re-factoring purposes... but as I said I don't hate it since I can just use the old syntax...
@josephizang6187
@josephizang6187 Год назад
Great Video Nick, a question is allocations especially the syntax that looks like a spread in javascript. Would this be performant?
@smallfox8623
@smallfox8623 Год назад
I looked at the IL and each "spread" compiles down to an iteration with the list's Enumerator along with an Add call for every element (exactly the same as foreach). So this List combined = [..list1, 7777, ..list2]; Is equivalent to List combined = new List(); foreach (var i in list1) { combined.Add(i); } combined.Add(7777); foreach (var i in list2) { combined.Add(i); }
@cheebadigga4092
@cheebadigga4092 Год назад
Love the Python-like syntax. But it's gonna add to the problem of having multiple ways of doing the same thing like timers for example. They should release a version which gets rid of all the old stuff at some point.
@frederikja2210
@frederikja2210 Год назад
Rather they remove the new stuff tbh. But they wont remove old stuff for backwards compatibility which im 1000%on board with
@cheebadigga4092
@cheebadigga4092 Год назад
@@frederikja2210 what? why remove the new stuff? Remove the old. If you release a new version of something that's your chance.
@LudwigvanBeethoven2
@LudwigvanBeethoven2 Год назад
A lot of existing code base would break and it would require a lot of work for a lot of developers to fix their code, which means a lot of time and money. They will never remove existing features from C#.
@cheebadigga4092
@cheebadigga4092 Год назад
@@LudwigvanBeethoven2 hence why I'm talking about releasing a new version. Python did it perfectly. Switching from version 2 to 3, everybody said the exact same thing as you first. Now everything Python is running on Python 3. So please...
@justinmarshall1930
@justinmarshall1930 Год назад
Nice feature, thanks for sharing Nick!
@Nekroido
@Nekroido Год назад
I really hope the main idea behind these new features is to simplify interop with F#. Passing collections between F# and C# packages is a bit of a pain in the ass rn
@DaliborHomola
@DaliborHomola Год назад
Basically, I see typescript. But I love it! ❤
@jonasgabrielmbn
@jonasgabrielmbn Год назад
From the thumbnail I thought they included list comprehension lol but we can only hope
@emmanueladebiyi2109
@emmanueladebiyi2109 Год назад
Love this feature a lot!
@madsen4617
@madsen4617 Год назад
Add list comprehensions and we are cooking.
@helleye311
@helleye311 Год назад
I like this. I think more languages should give better options to initialize collections. I'm not a C# programmer outside of some small uses, I mainly deal with javascript/typescript (I know, horrible etc), but the approach there of just typing in a fancy json object is amazing for manipulating collections. I hope this will work the same for non-assignment operations as well (ie. an inline return from a function. Compiler knows the type, since it has to match the function return, so it might work) I guess the implicit stackalloc might not be great, I'd leave the keyword in front of the array, just for clarity. Without having to do funky syntax or the full types it's still a lot better, even if that gets left in.
@tuanva6484
@tuanva6484 Год назад
I can't see any funny channel about Java like your channel
@Felipe-mg1pw
@Felipe-mg1pw Год назад
This is beautiful. They have to make these features in the final version.
@fynderde
@fynderde Год назад
I would prefer Dictionaries to use curly brackets { "a": 1, "b": 2}, since most people are already familiar with json and Dictionary is a completely different thing, which would this make obvious. Not sure how they would tell the compiler to differ between curly brackets and array syntax...
@AbhinavKulshreshtha
@AbhinavKulshreshtha Год назад
I am converting a php application into a dotnet application for a client. This would have made my work a lot easier. Unfortunately the client wants me to work on dotnet 6.
@nickst0ne
@nickst0ne Год назад
Spread operator: 👍 Collection/list/array initialization: 👍 ImmutableArray CollectionBuilder: wut? 🤨
@codeme8016
@codeme8016 Год назад
Just love the new syntax. Easier and cleaner which is the point
@JKhalaf
@JKhalaf Год назад
I love this personally, so much easier to work with!
@thyssenbot
@thyssenbot Год назад
Using two collections to initialize a new one is fine. But I doubt the utility of standalone initializers. Outside of demo foobar code, how many times do you encounter hard-coded collection initializers?
@anomalii7720
@anomalii7720 Год назад
Finally. Very nice !!
@lasindunuwanga5292
@lasindunuwanga5292 Год назад
That is very typescript and I love it… types and type combinations should be the next thing
@maxsamarin9002
@maxsamarin9002 Год назад
Looks nice! However when using other collections in initialization of others, the double dot prefixes seem redundant, don’t you think? Also it’s weird that array can be initialized with both square and squiggly brackets. Wouldn’t it be cleaner if they kept only the squiggly brackets and used them for initializing other collection types, instead of squares?
Далее
The New Inline Arrays of C# 12 Are Awesome, But…
8:16
The New Option and Result Types of C#
15:05
Просмотров 63 тыс.
3 .NET "Best Practices" I Changed My Mind About
10:16
Просмотров 103 тыс.
Primary Constructors in C# 12 Explained!
9:39
Просмотров 24 тыс.
6 C# Mistakes Microsoft Wants You to Fix
12:57
Просмотров 61 тыс.
Don't Use Polly in .NET Directly. Use this instead!
14:58
Projects Every Programmer Should Try
16:58
Просмотров 445 тыс.
I Feel Bad For New Programmers
19:12
Просмотров 438 тыс.
Why Most Programmers DON'T Last
18:56
Просмотров 306 тыс.
This Is Why Python Data Classes Are Awesome
22:19
Просмотров 803 тыс.
Brutally honest advice for new .NET Web Developers
7:19