The New Collection Access Feature of C# 13

Поделиться
HTML-код
  • Опубликовано: 28 апр 2024
  • Until the 30th of April, use code BIRTHDAY40 for 40% off any course, BIRTHDAY20 for 20% off any bundle and BIRTHDAY15 for 15% off your first year of Dometrain Pro: bit.ly/4aUVR8l
    Become a Patreon and get special perks: / nickchapsas
    Hello, everybody, I'm Nick, and in this video I will show you the brand new Implicit index access feature coming in C# 13 and .NET 9!
    Workshops: bit.ly/nickworkshops
    Don't forget to comment, like and subscribe :)
    Social Media:
    Follow me on GitHub: github.com/Elfocrash
    Follow me on Twitter: / nickchapsas
    Connect on LinkedIn: / nick-chapsas
    Keep coding merch: keepcoding.shop
    #csharp #dotnet

Комментарии • 124

  • @SirBenJamin_
    @SirBenJamin_ 27 дней назад +162

    I really enjoy your channel Nick, but I feel like lately, you're just making videos to advertise your website, ... the content is less useful than it used to be

    • @rowlul
      @rowlul 27 дней назад +21

      Yep, dometrain this dometrain that

    • @rosepainting8775
      @rosepainting8775 27 дней назад +10

      He has to support himself.

    • @honzajscz
      @honzajscz 27 дней назад +2

      I buy a course everything single time he says domtrain 😀

    • @rowlul
      @rowlul 27 дней назад

      @@IIARROWS I do have it but still 😅

    • @dosovi4548bustayes
      @dosovi4548bustayes 26 дней назад

      @@IIARROWS so true

  • @paulpach
    @paulpach 27 дней назад +225

    I can't think of a single time I would have used this feature.

    • @paulpach
      @paulpach 27 дней назад +9

      Hmmm, come to think of it, perhaps this would be useful to initialize sparse arrays? Where you want a bunch of zeroes except for a few elements? Still pretty esoteric use case.

    • @user-tk2jy8xr8b
      @user-tk2jy8xr8b 27 дней назад +5

      @@paulpach C#'s becoming an esolang

    • @Xamdify
      @Xamdify 27 дней назад +3

      @@paulpachit may be handy for serial protocols that require an end byte(s) where the size of the array is variable.
      But like you said, esoteric use cases.

    • @justinth83
      @justinth83 27 дней назад +24

      Honestly who is crying out for these features. I miss the Eric Lipert days when new language features were carefully considered and based on what would actually deliver a lot of value. We'd probably have discriminated unions by now but alas it looks like that's never going to happen. Don't mean to sound negative but it's a bit sad to see, although I do still enjoy coding in C#.

    • @berzurkfury
      @berzurkfury 27 дней назад +2

      All this looks like is the span code stuff getting copy pasted into anything that looks vaguely compatible

  • @CreativeB34ST
    @CreativeB34ST 27 дней назад +96

    Everytime Nick does his intro all I hear is "Hello everybody I'm naked."

    • @tedchirvasiu
      @tedchirvasiu 27 дней назад +5

      Hellovrybody

    • @Roddles
      @Roddles 27 дней назад +1

      So true 😂

    • @xNeoGenesis
      @xNeoGenesis 27 дней назад +1

      I have the suspicion that he knows what he is doing 😅

    • @doodidood
      @doodidood 27 дней назад +9

      Goddamn you I can't unhear it now

    • @alexclark6777
      @alexclark6777 27 дней назад +4

      Fun fact, he actually is naked when he records these. The shirt is digitally added in post-production.

  • @Denominus
    @Denominus 27 дней назад +38

    I'd love to know who asked for this and what big problem/pain it solved for them.

    • @gabrielhalvonik192
      @gabrielhalvonik192 27 дней назад +7

      maybe someone from language team wanted to distract work on semi-auto properties (field keyword) so we will not get it even this year

    • @jonasbarka
      @jonasbarka 27 дней назад +15

      It was me. I was drunk. Sorry.

  • @KingOfBlades27
    @KingOfBlades27 26 дней назад +2

    Was thinking the exact same you mentioned: not a lot of use for me, but those who work "lower" definitely might be more excited about this.

  • @hasmich
    @hasmich 27 дней назад +13

    I'm not sure this feature will be used a lot, but still it logically makes perfect sense that they added it, because it's what you would expect to happen. This improves the overall experience of using programming language.

  • @JanMitkowski
    @JanMitkowski 27 дней назад +2

    I remember seeing this ^1 for C# 8.0. VS was changing .lenght-1 to this notation.

  • @lordmetzgermeister
    @lordmetzgermeister 27 дней назад +3

    I recall a few times making an array, initializing it all to default and then setting a couple specific values using the indexer. I guess it would be a use case for this feature, but that must've been around the time of .NET Core 2.0 though.

  • @DaminGamerMC
    @DaminGamerMC 27 дней назад +15

    Im not a python developer only c# for now. Array[0] makes sense to me for the first element, but for some reason array[^1] also makes a lot of sense for the last element. I dont have srguments for it, i just see it as a logical thing somehow

    • @user-wp7we2vu6h
      @user-wp7we2vu6h 27 дней назад +14

      ^1 means length minus 1, just as 0 means beginning plus 0

    • @chris-pee
      @chris-pee 27 дней назад +5

      It kinda makes sense. If you consider [0] as the pointer to the start of an array (as in C), and the whole range contains the array from start to end. Then to get the last element you need to subtract the size of an element from the range-end pointer. Which is done by subtracting "1" in both C and now in C# somewhat.

    • @firestrm7
      @firestrm7 26 дней назад +1

      @@chris-pee And so C# 13 brings us back to think in assembly again :D

  • @xeroxeroxeroxeroxeroxeroxero
    @xeroxeroxeroxeroxeroxeroxero 24 дня назад

    That was a smooth transition to the LINQ course advert. I'm not even mad

  • @devincarlson8866
    @devincarlson8866 27 дней назад +3

    You’re finally using the new UI I see

    • @nickchapsas
      @nickchapsas  27 дней назад +5

      I couldn't take the peer pressure

  • @davidpine7
    @davidpine7 26 дней назад +3

    Random array of numbers... [ 6, 9, 4, 20 ]. 🤣

  • @izzyblackout1090
    @izzyblackout1090 27 дней назад +6

    How does it handles out of bound cases?

    • @mindstyler
      @mindstyler 26 дней назад +1

      Indexoutofboundsexception like always if course

  • @F1nalspace
    @F1nalspace 26 дней назад

    How to heck is this working, without no length specified in the array? Is the length automatically determined by the number of elements or by the actual indices?
    Don't get me wrong, i love index initializer for arrays and was waiting for it for so long (game/simulation dev) - but i don't see any use-case for this hat operator thingy.
    Why would i define something at the end? If i have an array for example that initializes a set of CPU instructions, the length of that instructions is finite - so i define the length beforehand and then map the instructions to the op-code index.
    Also one thing this bugs me a lot is the "Index" behind the scenes - which is also true for span.
    We work with many millions of data points, due to a high sample frequency and long time length. Right now, int-32 is sufficient, but we are already close to the limits of it.
    In the near future we really need a Span64 and Index64 or something like that, so we can use 64-bit indices and lengths directy - without the need to partition it.

  • @keyser456
    @keyser456 26 дней назад

    I've used it for doing simple parsing of multi-word commands input from the user in a game I'm working on:
    var splitAskTarget = fixedTarget.Split(' ');
    //excluding some validation to make sure >= 2 words
    string potentialAskTopic = splitAskTarget[^1]; // (single word topics only for now, last word)
    string shorterFixedTarget = string.Join(' ', splitAskTarget[..^2]); //start to 2nd to last word
    It's not pretty but no solution for something like this would be. It's niche, and I hate we're interchanging 0-based with 1-based in the same context of arrays, but it can save some keystrokes.

  • @EdKolis
    @EdKolis 26 дней назад

    Now that I've been using Android Studio at work for a while, this UI actually looks not unfamiliar to me. I need to get a copy of Rider for my C# projects!

  • @kakkarot1000
    @kakkarot1000 26 дней назад

    The "^" operator is called a "Caret" in some countries.

  • @alexclark6777
    @alexclark6777 27 дней назад +21

    Thank goodness they're working on these useful features instead of niche requests like discriminated unions.
    /s

    • @ralfwarnat9257
      @ralfwarnat9257 26 дней назад +2

      is that related to criminal records?

    • @alexclark6777
      @alexclark6777 26 дней назад +2

      @@ralfwarnat9257 No, those guys are in a protected class.

  • @Thial92
    @Thial92 23 дня назад

    Why tho ? Why try to reinvent the wheel with no real benefit other than obscuring what the code is actually doing behind various symbols ?

  • @rGunti
    @rGunti 27 дней назад +3

    Not knowing this operator from Python, I somehow find it more confusing that they opted for `^` instead of accepting negative numbers. Then again, I guess you can produce very strange bugs by passing in negative numbers and all that good stuff, so breaking that pattern intentionally may actually be a good idea.

    • @davr1
      @davr1 26 дней назад +5

      I can think of two reasons
      One is that indexOf returns -1 if no result is found, which would produce strange bugs like you mentioned
      Another is that range syntax like ^10..^5 would become too ambiguous with negative numbers: -10..-5 might as well be - (10..-5) with an unary operator

  • @7eper
    @7eper 16 дней назад

    Sad that ObservableCollection is not indexable.

    • @DoctorMGL
      @DoctorMGL 13 дней назад +2

      you can create your own custom ObservableCollection and just add this feature to it ,
      simply understand that : value[^1] is a short version of : value[ value.Length - 1 ]
      if you want to return by 2 , then change : value[ value.Length - 1 ] to value[ value.Length - 2 ]
      and so on . and instead of the number 1,2 .. extra create a parameter named Index and use call it back this way : return value[ value.Length - Index ] .

  • @onyxgc2568
    @onyxgc2568 27 дней назад +12

    c# having an identity crisis

  • @thelobo581
    @thelobo581 20 дней назад +1

    .net 8 is not a property of the solution though..😏

  • @antonmartyniuk
    @antonmartyniuk 27 дней назад +8

    Discriminated unions, please in C# 13

    • @user-zk5ym9ut1j
      @user-zk5ym9ut1j 27 дней назад

      Language team already responded like million times that this feature is not coming any time soon because of existing type system limitations

    • @diadetediotedio6918
      @diadetediotedio6918 27 дней назад

      ​@@user-zk5ym9ut1j
      Which are these limitations? I could made a simple system for DU in a package using source generators, what there is so special that they want to do here?

  • @paulovictor9439
    @paulovictor9439 27 дней назад +5

    Still no discriminated unions...

  • @johncarew1130
    @johncarew1130 26 дней назад +2

    Pretty sure that's known as a caret character, and not hat character.

    • @mindstyler
      @mindstyler 26 дней назад

      Is widely known as the hat character

    • @johncarew1130
      @johncarew1130 12 дней назад

      @@mindstyler you can try to recreate the wheel and give it a new name, but it will not change the fact that it is a wheel. The character is known as a caret character.

    • @mindstyler
      @mindstyler 12 дней назад

      @@johncarew1130 across programming it is almost always referred to as the hat character

    • @mindstyler
      @mindstyler 12 дней назад

      Originally it comes from mathematics. Only when it was adopted by Unicode that particular instance was named the caret. Google is your friend

  • @mranthonymills
    @mranthonymills 27 дней назад +4

    If anyone ever uses this feature they better have a DAMN good reason.

  • @Rick104547
    @Rick104547 27 дней назад +3

    Quite a niche use case, would rather see something like discriminated unions.

  • @yupii1997
    @yupii1997 26 дней назад

    To me this seems like a developer at microsoft needed this for a very very specific case, thus the reason it exists

  • @YahushuaMessiah777
    @YahushuaMessiah777 26 дней назад +1

    Let me tell how often I use arrays like this..............um, I don't! I use Lists and fill them with Linq.

  • @vin7524
    @vin7524 27 дней назад +2

    It is pity, that there is increasing amount of features, that are ... less useful ... for most of developers and they got priority over features that most devs miss. DUs, semi-auto properties (delayed for god know how long) and even anonymous interface implementations (which even language developers stated, that it will not be hard to implement, but we will never use that feature...for whatever reason...interest was not one of them according to proposal reactions)

  • @triGataro
    @triGataro 27 дней назад

    so Python #?

  • @fsharplove
    @fsharplove 27 дней назад +1

    A language with DU is really wanted by .NET developers and also a stable language. The result of adding too much features is that there is no idiomatic C# .
    General .NET developers are not aware that there are more than one language in .NET.

    • @justinth83
      @justinth83 27 дней назад +2

      Exactly this. Maybe we should just be thankful there isn't a new way to check for null in the next release?

    • @Sqrlmasta
      @Sqrlmasta 26 дней назад +2

      F# is a stable .NET language that supports discriminated unions... what's wrong with that?

    • @justinth83
      @justinth83 26 дней назад

      @@Sqrlmasta a good point and absolutely nothing wrong with F#, it's a great language. Curious though: assuming you use F# regularly, does it feel it gets the attention it deserves from MS or has it fallen by the wayside? I personally felt it never got the attention it deserved and has always been in C#'s shadow. I also seem to recall some recent features added to the .NET platform didn't play well with F# (sorry I forgot which exactly) which makes me wonder if MS do much testing or dog-fooding with F# these days (I would be surprised if they do).

  • @interrrp_with_three_rs
    @interrrp_with_three_rs 27 дней назад +5

    what the hell? they should have just used negative indices

  • @siposz
    @siposz 27 дней назад +6

    ^1 instead of ^0 is ultimate confusing

    • @catsandwich1259
      @catsandwich1259 27 дней назад +5

      Just think of '^X' being 'Length - X' and it makes sense. It keeps it consistent with the previous method.

    • @diadetediotedio6918
      @diadetediotedio6918 27 дней назад +5

      It is not and I'll make a simple argument for it:
      You would not write arr.length - 0 to access the last element of an array.
      In all of the languages that have this kind of feature you will also use arr[-1] to access the last element, this is logical and make sense because ultimately it is an easier way to write arr.length - 1.

    • @siposz
      @siposz 27 дней назад

      @@diadetediotedio6918
      for loop from the beggining:
      for (int i = 0; i

    • @diadetediotedio6918
      @diadetediotedio6918 27 дней назад +1

      @@siposz
      It is consistent of how you would do it without it:
      for(int i = arr.Length - 1; i >= 0; i--)
      arr[i]...
      You are just moving the arr.Length - 1 to i = 1 and

    • @siposz
      @siposz 27 дней назад

      @@diadetediotedio6918 IMHO it would be better with the exact same for loop, just add ^

  • @viktorstojanovic9007
    @viktorstojanovic9007 27 дней назад +9

    This gotta be the most useless feature they ever added

  • @jackkendall6420
    @jackkendall6420 27 дней назад +21

    The fact that they added this feature, instead of the specific and much-harder-to-implement feature I personally want, means C# is a bloated, poorly-designed, poorly-managed language that has no future

    • @fusedqyou
      @fusedqyou 27 дней назад +34

      My feature was not added, C# sucks!! 😡😡

    • @user-oy2bp9bq5m
      @user-oy2bp9bq5m 27 дней назад +2

      Did they cancelled your favourite feature or just not implemented yet? What's its name?

    • @marikselazemaj3428
      @marikselazemaj3428 27 дней назад +2

      Is it DU the feature you want?

    • @LeMustache
      @LeMustache 27 дней назад +1

      I feel so called out

    • @burger_flipper
      @burger_flipper 27 дней назад +6

      So much hate for nothing

  • @socar-pl
    @socar-pl 23 дня назад

    So it's "-1" from python, but as microsoft-usual less intuitive

  • @nithinb9671
    @nithinb9671 27 дней назад +9

    Such a pity for C#. They copied python to attract developers from different programming languages, but they forgot to maintain some professional standards, Everywhere, 0 is the starting index and here its different. Not sure if these Microsoft guys forgot about consistency or some kid did this without thinking of anything.

    • @kyokosaya
      @kyokosaya 27 дней назад +6

      The index ranging using hat has been in since C#8 though.

    • @shanehebert396
      @shanehebert396 27 дней назад +18

      It's not difficult to work it out if you think of the array as a circular array/buffer. Index [0] is the first element. Index [-1] is the element "before" element [0], which in a circular buffer or something would be the last element of the array.

    • @user-wp7we2vu6h
      @user-wp7we2vu6h 27 дней назад +12

      But why does indexing start from 0? Because of pointer arithmetic. Well then, it is logical that ^1 means length minus 1, just as 0 means beginning plus 0. This is based on banal convenience, so that you don’t have to constantly add or subtract one from the usual value.

    • @diadetediotedio6918
      @diadetediotedio6918 27 дней назад +8

      This is absolutely reasonable for them, you would not access the last element of an array normally by using array.Length - 0, you would use array.Length - 1, so array[^1] is perfectly fine by "professional standards".

  • @dzllz
    @dzllz 26 дней назад +2

    Clickbait, this seems useless, would have been interesting if you show what it’s good for

  • @Tony-dp1rl
    @Tony-dp1rl 21 день назад +2

    What a pointless addition to the language.

  • @jeroboam4486
    @jeroboam4486 27 дней назад

    It makes the code less readable, soon it's gonna look like Brainfuck! Linq is fine.

  • @TheOnlyDominik
    @TheOnlyDominik 27 дней назад +1

    I don't like this crappy syntax that makes the language complicated and unreadable. It only hurts to use this junk syntax “^1”. They should rather improve the compiler so that it optimizes the user code better for the CPU.

    • @hernanar3647
      @hernanar3647 27 дней назад

      But, they are doing that too. Is not that all new C# feature are like this, just a portion

    • @diadetediotedio6918
      @diadetediotedio6918 27 дней назад +3

      This is your opinion and you're entitled to it, but you are just spitting your feelings here, there's no value on them by themselves.
      There's absolutely nothing to do with compiler optimization here as well, and I don't think you know what you are saying.

    • @TheOnlyDominik
      @TheOnlyDominik 27 дней назад

      @@diadetediotedio6918 I think you are stupid!

    • @TheOnlyDominik
      @TheOnlyDominik 27 дней назад +1

      @@hernanar3647 It feels like every half year there are a few features that make the code unreadable and yes not all new features are shit.

    • @mindstyler
      @mindstyler 26 дней назад +1

      ​@@TheOnlyDominik'learning' is a word it would help to look up

  • @alfflasymphonyx
    @alfflasymphonyx 26 дней назад

    This is utterly useless feature.
    in addition, why use ^1 to indicate the last iitem in the array when we use 0 from the start. Two different way of indexing depending on where we start. Why?
    This is complete garbage!