The New Collection Access Feature of C# 13

  Рет қаралды 42,740

Nick Chapsas

Nick Chapsas

Ай бұрын

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

Пікірлер: 134
@SirBenJamin_
@SirBenJamin_ Ай бұрын
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 Ай бұрын
Yep, dometrain this dometrain that
@rosepainting8775
@rosepainting8775 Ай бұрын
He has to support himself.
@honzajscz
@honzajscz Ай бұрын
I buy a course everything single time he says domtrain 😀
@rowlul
@rowlul Ай бұрын
@@IIARROWS I do have it but still 😅
@dosovi4548bustayes
@dosovi4548bustayes Ай бұрын
@@IIARROWS so true
@paulpach
@paulpach Ай бұрын
I can't think of a single time I would have used this feature.
@paulpach
@paulpach Ай бұрын
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 Ай бұрын
@@paulpach C#'s becoming an esolang
@Xamdify
@Xamdify Ай бұрын
@@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 Ай бұрын
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 Ай бұрын
All this looks like is the span code stuff getting copy pasted into anything that looks vaguely compatible
@CreativeB34ST
@CreativeB34ST Ай бұрын
Everytime Nick does his intro all I hear is "Hello everybody I'm naked."
@tedchirvasiu
@tedchirvasiu Ай бұрын
Hellovrybody
@Roddles
@Roddles Ай бұрын
So true 😂
@xNeoGenesis
@xNeoGenesis Ай бұрын
I have the suspicion that he knows what he is doing 😅
@doodidood
@doodidood Ай бұрын
Goddamn you I can't unhear it now
@alexclark6777
@alexclark6777 Ай бұрын
Fun fact, he actually is naked when he records these. The shirt is digitally added in post-production.
@Denominus
@Denominus Ай бұрын
I'd love to know who asked for this and what big problem/pain it solved for them.
@gabrielhalvonik192
@gabrielhalvonik192 Ай бұрын
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 Ай бұрын
It was me. I was drunk. Sorry.
@djupstaten2328
@djupstaten2328 12 күн бұрын
Let's say you have a queue or stack-like abstraction and you want to populate the backing List/array to have some items queued first/last (depending on how they are popped/dequeued). Or you have a file-format you want to serialize to, which takes a 'footer', such as an IDAT-block, and you have these static elements prepared.
@KingOfBlades27
@KingOfBlades27 Ай бұрын
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.
@lordmetzgermeister
@lordmetzgermeister Ай бұрын
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.
@JanMitkowski
@JanMitkowski Ай бұрын
I remember seeing this ^1 for C# 8.0. VS was changing .lenght-1 to this notation.
@DaminGamerMC
@DaminGamerMC Ай бұрын
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 Ай бұрын
^1 means length minus 1, just as 0 means beginning plus 0
@chris-pee
@chris-pee Ай бұрын
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 Ай бұрын
@@chris-pee And so C# 13 brings us back to think in assembly again :D
@izzyblackout1090
@izzyblackout1090 Ай бұрын
How does it handles out of bound cases?
@mindstyler
@mindstyler Ай бұрын
Indexoutofboundsexception like always if course
@xeroxeroxeroxeroxeroxeroxero
@xeroxeroxeroxeroxeroxeroxero Ай бұрын
That was a smooth transition to the LINQ course advert. I'm not even mad
@hasmich
@hasmich Ай бұрын
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.
@chudchadanstud
@chudchadanstud 6 күн бұрын
It literally doesn't. It's bloat and it's unreadable. Now you have to stop and think about it whenever you read code and it's not even backwards compatible.
@hasmich
@hasmich 6 күн бұрын
​​​@@chudchadanstudboth object initialisers and indexes were already in the language and now they behave like you'd expect. Where's the bloat? You had to stop and think just to remember this limitation and wonder why is it so. Breaking change is when the code was working previously, but its behavior has changed - not this case, because it simply failed to compile.
@djupstaten2328
@djupstaten2328 12 күн бұрын
They should also copy tuple splatting and packing from python, i.e. multitype 'params' for tuples. It would be neat to be able to prepare argument-list separately from the function call/construction itself.
@F1nalspace
@F1nalspace Ай бұрын
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.
@devincarlson8866
@devincarlson8866 Ай бұрын
You’re finally using the new UI I see
@nickchapsas
@nickchapsas Ай бұрын
I couldn't take the peer pressure
@davidpine7
@davidpine7 Ай бұрын
Random array of numbers... [ 6, 9, 4, 20 ]. 🤣
@keyser456
@keyser456 Ай бұрын
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.
@kakkarot1000
@kakkarot1000 Ай бұрын
The "^" operator is called a "Caret" in some countries.
@EdKolis
@EdKolis Ай бұрын
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!
@Thial92
@Thial92 Ай бұрын
Why tho ? Why try to reinvent the wheel with no real benefit other than obscuring what the code is actually doing behind various symbols ?
@alexclark6777
@alexclark6777 Ай бұрын
Thank goodness they're working on these useful features instead of niche requests like discriminated unions. /s
@ralfwarnat9257
@ralfwarnat9257 Ай бұрын
is that related to criminal records?
@alexclark6777
@alexclark6777 Ай бұрын
@@ralfwarnat9257 No, those guys are in a protected class.
@rGunti
@rGunti Ай бұрын
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 Ай бұрын
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 Ай бұрын
Sad that ObservableCollection is not indexable.
@DoctorMGL
@DoctorMGL Ай бұрын
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 ] .
@sfjadi
@sfjadi 19 күн бұрын
Are these codes expired?
@bitmanagent67
@bitmanagent67 8 күн бұрын
They usually are expired after the date. When you set these up in the creator admin portal you add chose a product, in this case a course, add the regular sales price, add a discount campaign that includes the discount amount, code to enter at checkout, and closing date of the campaign. When the date passes, the codes no longer work.
@thelobo581
@thelobo581 Ай бұрын
.net 8 is not a property of the solution though..😏
@onyxgc2568
@onyxgc2568 Ай бұрын
c# having an identity crisis
@bitmanagent67
@bitmanagent67 8 күн бұрын
When you try to be all things to all people, you become useless to everyone. That being said, a lot of these language feature are syntactic sugar. Quality of life changes that allow more expressiveness for different approaches to problem solving. The compiler still turns for and foreach loops into while loops and this will still probably uses array length and index math under the covers. None of this stuff is better, just different. MSFT (just like any other company in the space) has to gain developer market share. New users keep their platform alive and relevant. So you want your product to support different coding styles and facilities found in other languages so that those people see merit and come on over. This realization does not mean any of us have to like it, we just need to understand it.
@antonmartyniuk
@antonmartyniuk Ай бұрын
Discriminated unions, please in C# 13
@user-zk5ym9ut1j
@user-zk5ym9ut1j Ай бұрын
Language team already responded like million times that this feature is not coming any time soon because of existing type system limitations
@diadetediotedio6918
@diadetediotedio6918 Ай бұрын
​@@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?
@johncarew1130
@johncarew1130 Ай бұрын
Pretty sure that's known as a caret character, and not hat character.
@mindstyler
@mindstyler Ай бұрын
Is widely known as the hat character
@johncarew1130
@johncarew1130 Ай бұрын
@@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 Ай бұрын
@@johncarew1130 across programming it is almost always referred to as the hat character
@mindstyler
@mindstyler Ай бұрын
Originally it comes from mathematics. Only when it was adopted by Unicode that particular instance was named the caret. Google is your friend
@paulovictor9439
@paulovictor9439 Ай бұрын
Still no discriminated unions...
@YahushuaMessiah777
@YahushuaMessiah777 Ай бұрын
Let me tell how often I use arrays like this..............um, I don't! I use Lists and fill them with Linq.
@mranthonymills
@mranthonymills Ай бұрын
If anyone ever uses this feature they better have a DAMN good reason.
@yupii1997
@yupii1997 Ай бұрын
To me this seems like a developer at microsoft needed this for a very very specific case, thus the reason it exists
@Rick104547
@Rick104547 Ай бұрын
Quite a niche use case, would rather see something like discriminated unions.
@triGataro
@triGataro Ай бұрын
so Python #?
@vin7524
@vin7524 Ай бұрын
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)
@fsharplove
@fsharplove Ай бұрын
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 Ай бұрын
Exactly this. Maybe we should just be thankful there isn't a new way to check for null in the next release?
@Sqrlmasta
@Sqrlmasta Ай бұрын
F# is a stable .NET language that supports discriminated unions... what's wrong with that?
@justinth83
@justinth83 Ай бұрын
@@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).
@siposz
@siposz Ай бұрын
^1 instead of ^0 is ultimate confusing
@catsandwich1259
@catsandwich1259 Ай бұрын
Just think of '^X' being 'Length - X' and it makes sense. It keeps it consistent with the previous method.
@diadetediotedio6918
@diadetediotedio6918 Ай бұрын
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 Ай бұрын
@@diadetediotedio6918 for loop from the beggining: for (int i = 0; i
@diadetediotedio6918
@diadetediotedio6918 Ай бұрын
@@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 Ай бұрын
@@diadetediotedio6918 IMHO it would be better with the exact same for loop, just add ^
@interrrp_with_three_rs
@interrrp_with_three_rs Ай бұрын
what the hell? they should have just used negative indices
@diadetediotedio6918
@diadetediotedio6918 Ай бұрын
It is the same as negative indices, lol. They just are not using the minus sign.
@interrrp_with_three_rs
@interrrp_with_three_rs Ай бұрын
@@diadetediotedio6918 exactly, why not
@nithinb9671
@nithinb9671 Ай бұрын
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 Ай бұрын
The index ranging using hat has been in since C#8 though.
@shanehebert396
@shanehebert396 Ай бұрын
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 Ай бұрын
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 Ай бұрын
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".
@socar-pl
@socar-pl Ай бұрын
So it's "-1" from python, but as microsoft-usual less intuitive
@viktorstojanovic9007
@viktorstojanovic9007 Ай бұрын
This gotta be the most useless feature they ever added
@Tony-dp1rl
@Tony-dp1rl Ай бұрын
What a pointless addition to the language.
@jackkendall6420
@jackkendall6420 Ай бұрын
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 Ай бұрын
My feature was not added, C# sucks!! 😡😡
@user-oy2bp9bq5m
@user-oy2bp9bq5m Ай бұрын
Did they cancelled your favourite feature or just not implemented yet? What's its name?
@marikselazemaj3428
@marikselazemaj3428 Ай бұрын
Is it DU the feature you want?
@LeMustache
@LeMustache Ай бұрын
I feel so called out
@burger_flipper
@burger_flipper Ай бұрын
So much hate for nothing
@chudchadanstud
@chudchadanstud 6 күн бұрын
What a useless feature. Just bloats the lang.
@jeroboam4486
@jeroboam4486 Ай бұрын
It makes the code less readable, soon it's gonna look like Brainfuck! Linq is fine.
@TheOnlyDominik
@TheOnlyDominik Ай бұрын
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 Ай бұрын
But, they are doing that too. Is not that all new C# feature are like this, just a portion
@diadetediotedio6918
@diadetediotedio6918 Ай бұрын
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 Ай бұрын
@@diadetediotedio6918 I think you are stupid!
@TheOnlyDominik
@TheOnlyDominik Ай бұрын
@@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 Ай бұрын
​@@TheOnlyDominik'learning' is a word it would help to look up
@alfflasymphonyx
@alfflasymphonyx Ай бұрын
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!
Swagger is Going Away in .NET 9!
10:48
Nick Chapsas
Рет қаралды 84 М.
6 INSANE Things You Didn't Know You Could Write in C#
12:26
Nick Chapsas
Рет қаралды 50 М.
A pack of chips with a surprise 🤣😍❤️ #demariki
00:14
Demariki
Рет қаралды 32 МЛН
WHO DO I LOVE MOST?
00:22
dednahype
Рет қаралды 14 МЛН
1❤️#thankyou #shorts
00:21
あみか部
Рет қаралды 71 МЛН
Sprinting with More and More Money
00:29
MrBeast
Рет қаралды 172 МЛН
.NET 9 Fixed Exceptions but Still Don’t Use Them
10:01
Nick Chapsas
Рет қаралды 39 М.
The New Extensions EVERYTHING Feature of C# 13!
10:32
Nick Chapsas
Рет қаралды 68 М.
You Need to Update Your .NET Solution Files!
6:59
Nick Chapsas
Рет қаралды 74 М.
Interview With A Sr JavaScript Dev | Prime Reacts
24:43
ThePrimeTime
Рет қаралды 165 М.
Make Your LINQ Up to 10x Faster!
11:08
Nick Chapsas
Рет қаралды 53 М.
"Stop Using Async Await in .NET to Save Threads" | Code Cop #018
14:05
Microsoft FINALLY killed it
6:45
Alex Ziskind
Рет қаралды 553 М.
"Stop Using Properties in C#, Just Use Fields" | Code Cop #013
11:53
A pack of chips with a surprise 🤣😍❤️ #demariki
00:14
Demariki
Рет қаралды 32 МЛН