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

แชร์
ฝัง
  • เผยแพร่เมื่อ 4 ต.ค. 2024
  • 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

ความคิดเห็น • 324

  • @Palladin007
    @Palladin007 ปีที่แล้ว +318

    The code with the span does not use a stackalloc. It is an array as span. SharpLab can show you

    • @nickchapsas
      @nickchapsas  ปีที่แล้ว +165

      You're right, I totally missed that! Pinning this so people can see it.

    • @Krimog
      @Krimog ปีที่แล้ว +51

      Maybe the should add a Span s = stackalloc [1, 2, 3]; feature?

    • @Krimog
      @Krimog ปีที่แล้ว +9

      @@Khitiara_ The idea behind Collection Literals is to simplify it and make it consistent.

    • @Khitiara_
      @Khitiara_ ปีที่แล้ว +6

      @@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_ ปีที่แล้ว +5

      @@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 ปีที่แล้ว +16

    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 ปีที่แล้ว +124

    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 ปีที่แล้ว +12

      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 10 หลายเดือนก่อน

      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 ปีที่แล้ว +69

    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 ปีที่แล้ว +1

      I think there can be language parser issues with this approach, and that's why they are trying to find something else

    • @ElCerdoBlanco
      @ElCerdoBlanco ปีที่แล้ว +4

      This would also resemble the JSON syntax, which I think is the basis for most languages, that do it the same way.

  • @Vastlee
    @Vastlee ปีที่แล้ว +11

    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 ปีที่แล้ว

      In case the team ever wants to do block expressions, List nums = { 1 }; could be ambiguous.

  • @andy010
    @andy010 ปีที่แล้ว +34

    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 ปีที่แล้ว +8

      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

  • @CharlesBurnsPrime
    @CharlesBurnsPrime ปีที่แล้ว +34

    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.

  • @klocugh12
    @klocugh12 ปีที่แล้ว +43

    I really hope this makes it into official version.

  • @OllyWood688
    @OllyWood688 ปีที่แล้ว +9

    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

  • @ciberman
    @ciberman ปีที่แล้ว +12

    I almost cried of joy when I saw the intro

  • @maskettaman1488
    @maskettaman1488 ปีที่แล้ว +8

    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

  • @jameseze9469
    @jameseze9469 ปีที่แล้ว +1

    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.

  • @MyriadColorsCM
    @MyriadColorsCM ปีที่แล้ว +6

    Honestly, this will be such a huge help in making C# code more readable.
    Its a great feature.

  • @janbodnar7815
    @janbodnar7815 ปีที่แล้ว +21

    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#.

  • @ryan-heath
    @ryan-heath ปีที่แล้ว +1

    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)
    {
    }

  • @alfany6252
    @alfany6252 ปีที่แล้ว +7

    FINALLY!! Even F# has it's own literals for different collections.

  • @billy65bob
    @billy65bob ปีที่แล้ว +9

    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 ปีที่แล้ว +4

      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.

  • @smuddy
    @smuddy ปีที่แล้ว +5

    As a typescript/C# dev i really appreciate these changes.

  • @r-naotwo6290
    @r-naotwo6290 ปีที่แล้ว +8

    I love it and I hope the proposed one for the dictionary is maintained. Thanks!

  • @nomadshiba
    @nomadshiba ปีที่แล้ว +1

    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.

  • @bsoter
    @bsoter ปีที่แล้ว +2

    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 ปีที่แล้ว +9

    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 ปีที่แล้ว +1

      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 ปีที่แล้ว +5

    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.

  • @fr3ddyfr3sh
    @fr3ddyfr3sh ปีที่แล้ว +2

    Finally!
    Learned programming with Python, worked with c# for 10 years. so happy to see this.

  • @der.Schtefan
    @der.Schtefan ปีที่แล้ว +31

    Using [ ] as syntax is what they should have done in the first place.

  • @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.

  • @StarfoxHUN
    @StarfoxHUN ปีที่แล้ว +2

    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.

  • @Yous0147
    @Yous0147 ปีที่แล้ว +1

    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.

  • @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.

  • @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?

  • @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.

  • @Herio7
    @Herio7 ปีที่แล้ว +1

    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.

  • @Subjective0
    @Subjective0 ปีที่แล้ว +2

    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.

  • @UrzaRage778
    @UrzaRage778 ปีที่แล้ว +1

    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!

  • @omriliad659
    @omriliad659 ปีที่แล้ว +1

    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

  • @dennycrane2938
    @dennycrane2938 ปีที่แล้ว +2

    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.

  • @oscareriksson9414
    @oscareriksson9414 ปีที่แล้ว +3

    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

  • @viktorstojanovic9007
    @viktorstojanovic9007 ปีที่แล้ว +7

    Why didnt they just use {} for everything not just arrays

  • @klaxxon__
    @klaxxon__ ปีที่แล้ว

    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 ปีที่แล้ว +4

    Yeah, too many ways to do the exact same thing.
    Atm I feel like getting diabetes from all the syntactic sugar in C#.

  • @EudaderurScheiss
    @EudaderurScheiss ปีที่แล้ว +1

    i love that c# is like, lets steal all the good stuff

  • @mykhailokozlov6641
    @mykhailokozlov6641 ปีที่แล้ว +13

    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 ปีที่แล้ว +1

      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 ปีที่แล้ว +2

      @@smallfox8623 The soon for DUs has easily been passed by now imo

    • @Felipe-mg1pw
      @Felipe-mg1pw ปีที่แล้ว +1

      I would love union types. So, you don't always have to create multiple fields where only one of them is used afterwards

  • @antonmartyniuk
    @antonmartyniuk ปีที่แล้ว +2

    It looks like JavaScript or TypeScript and I absolutely love it

  • @The12thAvenger
    @The12thAvenger ปีที่แล้ว +2

    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.

  • @MichaelBattaglia
    @MichaelBattaglia ปีที่แล้ว +2

    I don't mind pulling features from other languages. As long as C# doesn't make white space characters as Python!

  • @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++

  • @NoName-1337
    @NoName-1337 ปีที่แล้ว +2

    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 :)

  • @dmytroyehorov2905
    @dmytroyehorov2905 ปีที่แล้ว +3

    In F# this already implemented. Square brackets for lists, square with | for arrays.

    • @VoroninPavel
      @VoroninPavel ปีที่แล้ว

      yet no literal for the dictionary/map

  • @betterlifeexe4378
    @betterlifeexe4378 ปีที่แล้ว +2

    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.

  • @MiSt3300
    @MiSt3300 ปีที่แล้ว

    Im very glad of these changes, it's much more readable, it looks more like typescript

  • @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.

  • @benesste
    @benesste ปีที่แล้ว +1

    I did not know that the List does have a Dump method.

    • @nickchapsas
      @nickchapsas  ปีที่แล้ว +2

      It doesn’t. It’s a sharplab thing

    • @RobinHood70
      @RobinHood70 ปีที่แล้ว +1

      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????

  • @hemant-sathe
    @hemant-sathe ปีที่แล้ว +1

    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 ปีที่แล้ว +1

      I see your point but disagree, as it would diverge from list pattern matching syntax

  • @parlor3115
    @parlor3115 ปีที่แล้ว +5

    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 ปีที่แล้ว +1

      ​@@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 :)

  • @mohan7273
    @mohan7273 ปีที่แล้ว +1

    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?

  • @saeedbarari2207
    @saeedbarari2207 ปีที่แล้ว +5

    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 ปีที่แล้ว +1

      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.

  • @cheebadigga4092
    @cheebadigga4092 ปีที่แล้ว +5

    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...

  • @the-avid-engineer
    @the-avid-engineer ปีที่แล้ว +5

    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)

  • @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?

  • @no-bc4kc
    @no-bc4kc ปีที่แล้ว

    This looks really interesting and I for one am looking forward to using it

  • @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

  • @emerynoel567
    @emerynoel567 ปีที่แล้ว

    I like it. Very similar to JS, at least in syntax. Love the spread-style operator, too.

  • @T33K3SS3LCH3N
    @T33K3SS3LCH3N ปีที่แล้ว

    C: I am basically every programming language because of my macros
    C#: I'm literally every programming language.

  • @Felipe-mg1pw
    @Felipe-mg1pw ปีที่แล้ว

    This is beautiful. They have to make these features in the final version.

  • @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 [])

  • @antosha2224
    @antosha2224 ปีที่แล้ว

    Omg finally c# designers add useful simple way to creating collection objects.

  • @MirrorsEdgeGamer01
    @MirrorsEdgeGamer01 ปีที่แล้ว +1

    It’s looking similar to how Swift initialises dictionaries and arrays.

  • @justinmarshall1930
    @justinmarshall1930 ปีที่แล้ว

    Nice feature, thanks for sharing Nick!

  • @josephizang6187
    @josephizang6187 ปีที่แล้ว +3

    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);
      }

  • @thedreadedgman
    @thedreadedgman ปีที่แล้ว +2

    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...

  • @Crozz22
    @Crozz22 ปีที่แล้ว +3

    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.

  • @lasindunuwanga5292
    @lasindunuwanga5292 ปีที่แล้ว

    That is very typescript and I love it… types and type combinations should be the next thing

  • @emmanueladebiyi2109
    @emmanueladebiyi2109 ปีที่แล้ว

    Love this feature a lot!

  • @GabrielGonzalez2
    @GabrielGonzalez2 ปีที่แล้ว +1

    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?

  • @nthonymiller
    @nthonymiller ปีที่แล้ว

    Love it, just like javascript/typescript. I wish the spread was three dots not two.

  • @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" } };

  • @DaliborHomola
    @DaliborHomola ปีที่แล้ว

    Basically, I see typescript. But I love it! ❤

  • @logank.70
    @logank.70 ปีที่แล้ว +13

    It feels like C# has been slowly turning into a hybrid language. It's becoming more and more a language that better supports a functional style. I like this feature (with those similar caveats) and most others but it feels like the language has too many features.

    • @bluecup25
      @bluecup25 ปีที่แล้ว +7

      I prefer to have options than limitations

    • @logank.70
      @logank.70 ปีที่แล้ว +3

      @@bluecup25
      I can absolutely get behind that thought. The only thing I feel is an issue is how it can be dizzying how many options there are. Almost like teams have to decide what options they don't want to use for consistency reasons.

    • @bluecup25
      @bluecup25 ปีที่แล้ว +1

      @@logank.70 True. Also I like the fast progress of C# but I hope they put plenty of thought into new features and don't create problems impossible to solve in the future (or with ugly workarounds to maintain backwards compatibility).

  • @xavier.xiques
    @xavier.xiques ปีที่แล้ว

    I was waiting for this a long time 👏

  • @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.

  • @jonasgabrielmbn
    @jonasgabrielmbn ปีที่แล้ว

    From the thumbnail I thought they included list comprehension lol but we can only hope

  • @madsen4617
    @madsen4617 ปีที่แล้ว

    Add list comprehensions and we are cooking.

  • @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.

  • @JKhalaf
    @JKhalaf ปีที่แล้ว

    I love this personally, so much easier to work with!

  • @CreativeB34ST
    @CreativeB34ST ปีที่แล้ว +2

    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.

  • @iSpeakItNae
    @iSpeakItNae ปีที่แล้ว

    I was thinking this the other day, using top-level statements only.

  • @matthias916
    @matthias916 ปีที่แล้ว +2

    I love how C# is adding more and more functional syntactical sugar

  • @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.

  • @salameez
    @salameez ปีที่แล้ว

    Really wish it would be smart enough to understand something like List strings = [1, 2, 3].Select(i => i.ToString()).ToList();

  • @tuanva6484
    @tuanva6484 ปีที่แล้ว

    I can't see any funny channel about Java like your channel

  • @SkraxsChannel
    @SkraxsChannel ปีที่แล้ว

    I don’t really like the change, but I can see how this improves typing speed. I don’t find the new form simpler to read or the old one harder. It’s just shorter.

  • @EPK_AI_DUBS
    @EPK_AI_DUBS ปีที่แล้ว

    Noce video. Nick,when are you going to try and make a video about the new GPT code interpreter? It's very powerful

  • @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.

  • @aaronkoller8811
    @aaronkoller8811 ปีที่แล้ว

    I love this feature. Square brackets for me are just fine. Spread operators FTW!

  • @gentlemanbirdlake
    @gentlemanbirdlake ปีที่แล้ว

    wake me when they get to comprehensions, they are the killer feature of python syntax.

  • @hlazunov
    @hlazunov ปีที่แล้ว

    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.

  • @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

  • @oortcloud210
    @oortcloud210 ปีที่แล้ว

    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}}}
    """;