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

  Рет қаралды 103,926

Nick Chapsas

Nick Chapsas

10 ай бұрын

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/dotnet/csharplang/...
Workshops: bit.ly/nickworkshops
Don't forget to comment, like and subscribe :)
Social Media:
Follow me on GitHub: bit.ly/ChapsasGitHub
Follow me on Twitter: bit.ly/ChapsasTwitter
Connect on LinkedIn: bit.ly/ChapsasLinkedIn
Keep coding merch: keepcoding.shop
#csharp #dotnet

Пікірлер: 322
@Palladin007
@Palladin007 10 ай бұрын
The code with the span does not use a stackalloc. It is an array as span. SharpLab can show you
@nickchapsas
@nickchapsas 10 ай бұрын
You're right, I totally missed that! Pinning this so people can see it.
@Krimog
@Krimog 10 ай бұрын
Maybe the should add a Span s = stackalloc [1, 2, 3]; feature?
@Krimog
@Krimog 10 ай бұрын
@@Khitiara_ The idea behind Collection Literals is to simplify it and make it consistent.
@Khitiara_
@Khitiara_ 10 ай бұрын
@@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_ 10 ай бұрын
@@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 9 ай бұрын
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 10 ай бұрын
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 10 ай бұрын
JS spread operator is also a common performance bottleneck, so use responsibly :)
@fifty-plus
@fifty-plus 10 ай бұрын
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 10 ай бұрын
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 5 ай бұрын
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.
@CharlesBurnsPrime
@CharlesBurnsPrime 10 ай бұрын
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.
@marna_li
@marna_li 10 ай бұрын
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 10 ай бұрын
I think there can be language parser issues with this approach, and that's why they are trying to find something else
@ElCerdoBlanco
@ElCerdoBlanco 10 ай бұрын
This would also resemble the JSON syntax, which I think is the basis for most languages, that do it the same way.
@klocugh12
@klocugh12 10 ай бұрын
I really hope this makes it into official version.
@maskettaman1488
@maskettaman1488 10 ай бұрын
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 10 ай бұрын
I almost cried of joy when I saw the intro
@andy010
@andy010 10 ай бұрын
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 10 ай бұрын
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
@janbodnar7815
@janbodnar7815 10 ай бұрын
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#.
@r-naotwo6290
@r-naotwo6290 10 ай бұрын
I love it and I hope the proposed one for the dictionary is maintained. Thanks!
@Vastlee
@Vastlee 10 ай бұрын
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 8 ай бұрын
In case the team ever wants to do block expressions, List nums = { 1 }; could be ambiguous.
@OllyWood688
@OllyWood688 10 ай бұрын
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 10 ай бұрын
xD Yeah, a couple of years later nobody will infer that in mind :D
@Mentox2
@Mentox2 10 ай бұрын
Honestly, this will be such a huge help in making C# code more readable. Its a great feature.
@Yous0147
@Yous0147 10 ай бұрын
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.
@alfany6252
@alfany6252 10 ай бұрын
FINALLY!! Even F# has it's own literals for different collections.
@StarfoxHUN
@StarfoxHUN 10 ай бұрын
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.
@smuddy
@smuddy 9 ай бұрын
As a typescript/C# dev i really appreciate these changes.
@justinmarshall1930
@justinmarshall1930 10 ай бұрын
Nice feature, thanks for sharing Nick!
@no-bc4kc
@no-bc4kc 10 ай бұрын
This looks really interesting and I for one am looking forward to using it
@der.Schtefan
@der.Schtefan 10 ай бұрын
Using [ ] as syntax is what they should have done in the first place.
@1992jamo
@1992jamo 9 ай бұрын
That looks fantastic!
@xavier.xiques
@xavier.xiques 10 ай бұрын
I was waiting for this a long time 👏
@emerynoel567
@emerynoel567 10 ай бұрын
I like it. Very similar to JS, at least in syntax. Love the spread-style operator, too.
@oscareriksson9414
@oscareriksson9414 10 ай бұрын
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
@aoisonne1461
@aoisonne1461 9 ай бұрын
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?
@ryan-heath
@ryan-heath 10 ай бұрын
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) { }
@klaxxon__
@klaxxon__ 7 ай бұрын
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.
@nomadshiba
@nomadshiba 9 ай бұрын
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.
@emmanueladebiyi2109
@emmanueladebiyi2109 10 ай бұрын
Love this feature a lot!
@fr3ddyfr3sh
@fr3ddyfr3sh 10 ай бұрын
Finally! Learned programming with Python, worked with c# for 10 years. so happy to see this.
@EPK_AI_DUBS
@EPK_AI_DUBS 10 ай бұрын
Noce video. Nick,when are you going to try and make a video about the new GPT code interpreter? It's very powerful
@omriliad659
@omriliad659 10 ай бұрын
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)
@Felipe-mg1pw
@Felipe-mg1pw 10 ай бұрын
This is beautiful. They have to make these features in the final version.
@jameseze9469
@jameseze9469 10 ай бұрын
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.
@NoName-1337
@NoName-1337 10 ай бұрын
As also a TS dev, I appreciate this feature.
@josephizang6187
@josephizang6187 10 ай бұрын
Great Video Nick, a question is allocations especially the syntax that looks like a spread in javascript. Would this be performant?
@smallfox8623
@smallfox8623 10 ай бұрын
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); }
@MiSt3300
@MiSt3300 9 ай бұрын
Im very glad of these changes, it's much more readable, it looks more like typescript
@JKhalaf
@JKhalaf 10 ай бұрын
I love this personally, so much easier to work with!
@bsoter
@bsoter 10 ай бұрын
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.
@cypher9000
@cypher9000 10 ай бұрын
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.
@Herio7
@Herio7 10 ай бұрын
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.
@nanvlad
@nanvlad 10 ай бұрын
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 10 ай бұрын
I'd special case empty immutable (not-addable) collections and intern them
@Blu3Souls
@Blu3Souls 10 ай бұрын
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.
@redradist
@redradist 10 ай бұрын
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++
@Mitakbacktrack
@Mitakbacktrack 10 ай бұрын
Very nice!
@Subjective0
@Subjective0 10 ай бұрын
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.
@guillermomazzari8320
@guillermomazzari8320 10 ай бұрын
I liked a lot how to concat lists, it is just beautiful
@UrzaRage778
@UrzaRage778 10 ай бұрын
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!
@lordicemaniac
@lordicemaniac 10 ай бұрын
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 [])
@hosseinghahari3557
@hosseinghahari3557 10 ай бұрын
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
@CarrotCakeMake
@CarrotCakeMake 10 ай бұрын
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.
@escribe6751
@escribe6751 10 ай бұрын
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 10 ай бұрын
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.
@anomalii7720
@anomalii7720 10 ай бұрын
Finally. Very nice !!
@Kingside88
@Kingside88 10 ай бұрын
Cool presentation Nick. I like this new feature and it looks very clean and readable. Except the dictionary one. I'm not sure if this is a good way to go
@lasindunuwanga5292
@lasindunuwanga5292 10 ай бұрын
That is very typescript and I love it… types and type combinations should be the next thing
@ricardodegenova
@ricardodegenova 10 ай бұрын
Wow! I really like these changes coming to CSharp! I think those are good features overall and don't really dilute CSharp at all.
@dennycrane2938
@dennycrane2938 10 ай бұрын
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.
@EudaderurScheiss
@EudaderurScheiss 10 ай бұрын
i love that c# is like, lets steal all the good stuff
@maxsamarin9002
@maxsamarin9002 10 ай бұрын
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?
@antonmartyniuk
@antonmartyniuk 10 ай бұрын
It looks like JavaScript or TypeScript and I absolutely love it
@MrFreddao
@MrFreddao 7 ай бұрын
This looks good. i like it.
@helleye311
@helleye311 10 ай бұрын
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.
@mohan7273
@mohan7273 10 ай бұрын
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?
@owensigurdson8610
@owensigurdson8610 10 ай бұрын
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.
@aaronkoller8811
@aaronkoller8811 10 ай бұрын
I love this feature. Square brackets for me are just fine. Spread operators FTW!
@Nekroido
@Nekroido 8 ай бұрын
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
@billy65bob
@billy65bob 10 ай бұрын
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 10 ай бұрын
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.
@nasmifive
@nasmifive 9 ай бұрын
Is it not a direct copying from the features of JS and ES6, except for 3-dots vs 2-dots?
@mykhailokozlov6641
@mykhailokozlov6641 10 ай бұрын
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 10 ай бұрын
OneOf for unions, inheritance w/ClosedTypeHierarchyDiagnosticSuppressor for discriminated unions. Don't mind me just puking in the corner
@smallfox8623
@smallfox8623 10 ай бұрын
There is at least one proposal for type unions so I imagine it will happen one day relatively soon
@z0nx
@z0nx 10 ай бұрын
@@smallfox8623 The soon for DUs has easily been passed by now imo
@Felipe-mg1pw
@Felipe-mg1pw 10 ай бұрын
I would love union types. So, you don't always have to create multiple fields where only one of them is used afterwards
@EzequielRegaldo
@EzequielRegaldo 10 ай бұрын
Feels like JS, awesome !
@DaliborHomola
@DaliborHomola 10 ай бұрын
Basically, I see typescript. But I love it! ❤
@codeme8016
@codeme8016 10 ай бұрын
Just love the new syntax. Easier and cleaner which is the point
@hemant-sathe
@hemant-sathe 10 ай бұрын
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 10 ай бұрын
I see your point but disagree, as it would diverge from list pattern matching syntax
@DarraghJones
@DarraghJones 10 ай бұрын
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 10 ай бұрын
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 10 ай бұрын
@@smallfox8623 ok, fair enough. ignore me so :)
@zbaktube
@zbaktube 10 ай бұрын
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?
@sealsharp
@sealsharp 10 ай бұрын
I would have preferred the curly brackets {syntax} from arrays instead of [ ]. 05:20 Curious how that is implemented below the surface. It can be done in a way that wastes memory or not.
@ryan-heath
@ryan-heath 10 ай бұрын
Current implementation initializes the list without a predetermined size, and it adds each value one per one to the list. So yes, not very optimal at the moment ...
@Krakkk
@Krakkk 10 ай бұрын
Will we get ranges/indices for List?
@Crozz22
@Crozz22 10 ай бұрын
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 10 ай бұрын
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.
@GabrielGonzalez2
@GabrielGonzalez2 10 ай бұрын
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?
@hlazunov
@hlazunov 10 ай бұрын
Another feature that I would like to see is multiple return types (like Go does). Instead of declaring a tuple explicitly to make it more native-like.
@lucavalentino
@lucavalentino 9 ай бұрын
And what about Sets? Can we also create them in a similar Pythonic way?
@betterlifeexe4378
@betterlifeexe4378 10 ай бұрын
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.
@nthonymiller
@nthonymiller 10 ай бұрын
Love it, just like javascript/typescript. I wish the spread was three dots not two.
@sealkeen
@sealkeen 10 ай бұрын
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.
@romanhrytskiv6280
@romanhrytskiv6280 10 ай бұрын
amazing
@abondbrooks
@abondbrooks 9 ай бұрын
I was thinking this the other day, using top-level statements only.
@MrAbrazildo
@MrAbrazildo 10 ай бұрын
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" } };
@fynderde
@fynderde 8 ай бұрын
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...
@benesste
@benesste 10 ай бұрын
I did not know that the List does have a Dump method.
@nickchapsas
@nickchapsas 10 ай бұрын
It doesn’t. It’s a sharplab thing
@RobinHood70
@RobinHood70 10 ай бұрын
I was about to say the same thing, but with Nick's reply, I think "Dammit!" is more appropriate. 😛
@petrzurek5713
@petrzurek5713 10 ай бұрын
Shame there is no proposal for this feature. Or is there????
@cheebadigga4092
@cheebadigga4092 10 ай бұрын
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 10 ай бұрын
Rather they remove the new stuff tbh. But they wont remove old stuff for backwards compatibility which im 1000%on board with
@cheebadigga4092
@cheebadigga4092 10 ай бұрын
@@frederikja2210 what? why remove the new stuff? Remove the old. If you release a new version of something that's your chance.
@LudwigvanBeethoven2
@LudwigvanBeethoven2 10 ай бұрын
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 10 ай бұрын
@@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...
@oortcloud210
@oortcloud210 10 ай бұрын
Not too bad - at least it's easy to read. I find this obsession adding more and more syntax in order to create less and less code gets a little over done at times and we end up with very difficult syntax to read with way too many ways to perform the same task, but in this case it seems fine and just simplifies. An example of what I didn't like was raw string literals in 11. Even the example on the MS docs makes my eyes bleed: var location = $$""" You are at {{{Longitude}}, {{Latitude}}} """;
@dmytroyehorov2905
@dmytroyehorov2905 10 ай бұрын
In F# this already implemented. Square brackets for lists, square with | for arrays.
@VoroninPavel
@VoroninPavel 10 ай бұрын
yet no literal for the dictionary/map
@MichaelBattaglia
@MichaelBattaglia 10 ай бұрын
I don't mind pulling features from other languages. As long as C# doesn't make white space characters as Python!
@The12thAvenger
@The12thAvenger 10 ай бұрын
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.
@Maskedmous
@Maskedmous 10 ай бұрын
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.
@isnotnull
@isnotnull 10 ай бұрын
At last we have hope :)
@VaragornX
@VaragornX 10 ай бұрын
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 10 ай бұрын
Just program in C bro
@thyssenbot
@thyssenbot 10 ай бұрын
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?
@MoskowGoAwayFromUa
@MoskowGoAwayFromUa 10 ай бұрын
Nice, collection initialization is a pain. Especially dictionaries. I like python style - no need to type many symbols and no need to read a lot of symbols after than
The New Inline Arrays of C# 12 Are Awesome, But…
8:16
Nick Chapsas
Рет қаралды 31 М.
Every New Feature Added in C# 12
14:19
Nick Chapsas
Рет қаралды 146 М.
КИРПИЧ ОБ ГОЛОВУ #shorts
00:24
Паша Осадчий
Рет қаралды 6 МЛН
Mini Jelly Cake 🎂
00:50
Mr. Clabik
Рет қаралды 17 МЛН
Stop Using the Worst Way to Loop Lists in .NET!
9:35
Nick Chapsas
Рет қаралды 40 М.
The fastest way to iterate a List in C# is NOT what you think
13:42
Nick Chapsas
Рет қаралды 154 М.
delegate in C# | How it Works
27:33
TechBuddy EN
Рет қаралды 391
Why Startups Hate .NET and C#
10:38
Nick Chapsas
Рет қаралды 238 М.
The Right Way to Check for Null in C#
9:35
Nick Chapsas
Рет қаралды 93 М.
The New Best Way To Search for Values in .NET 8
11:05
Nick Chapsas
Рет қаралды 68 М.
"Stop Wasting Memory on Strings in C#!" | Code Cop #016
9:20
Nick Chapsas
Рет қаралды 66 М.
Why is C# Evolving This Way?
15:02
Zoran Horvat
Рет қаралды 19 М.
What Is .NET Aspire? The Insane Future of .NET!
18:35
Nick Chapsas
Рет қаралды 254 М.
КИРПИЧ ОБ ГОЛОВУ #shorts
00:24
Паша Осадчий
Рет қаралды 6 МЛН