The New Collection Access Feature of C# 13

  Рет қаралды 40,963

Nick Chapsas

Nick Chapsas

23 күн бұрын

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_ 21 күн бұрын
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 21 күн бұрын
Yep, dometrain this dometrain that
@rosepainting8775
@rosepainting8775 21 күн бұрын
He has to support himself.
@honzajscz
@honzajscz 21 күн бұрын
I buy a course everything single time he says domtrain 😀
@rowlul
@rowlul 21 күн бұрын
@@IIARROWS I do have it but still 😅
@dosovi4548bustayes
@dosovi4548bustayes 21 күн бұрын
@@IIARROWS so true
@paulpach
@paulpach 21 күн бұрын
I can't think of a single time I would have used this feature.
@paulpach
@paulpach 21 күн бұрын
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 21 күн бұрын
@@paulpach C#'s becoming an esolang
@Xamdify
@Xamdify 21 күн бұрын
@@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 21 күн бұрын
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 21 күн бұрын
All this looks like is the span code stuff getting copy pasted into anything that looks vaguely compatible
@CreativeB34ST
@CreativeB34ST 21 күн бұрын
Everytime Nick does his intro all I hear is "Hello everybody I'm naked."
@tedchirvasiu
@tedchirvasiu 21 күн бұрын
Hellovrybody
@Roddles
@Roddles 21 күн бұрын
So true 😂
@xNeoGenesis
@xNeoGenesis 21 күн бұрын
I have the suspicion that he knows what he is doing 😅
@doodidood
@doodidood 21 күн бұрын
Goddamn you I can't unhear it now
@alexclark6777
@alexclark6777 21 күн бұрын
Fun fact, he actually is naked when he records these. The shirt is digitally added in post-production.
@Denominus
@Denominus 21 күн бұрын
I'd love to know who asked for this and what big problem/pain it solved for them.
@gabrielhalvonik192
@gabrielhalvonik192 21 күн бұрын
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 21 күн бұрын
It was me. I was drunk. Sorry.
@KingOfBlades27
@KingOfBlades27 21 күн бұрын
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 21 күн бұрын
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.
@hasmich
@hasmich 21 күн бұрын
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 21 күн бұрын
I remember seeing this ^1 for C# 8.0. VS was changing .lenght-1 to this notation.
@DaminGamerMC
@DaminGamerMC 21 күн бұрын
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 21 күн бұрын
^1 means length minus 1, just as 0 means beginning plus 0
@chris-pee
@chris-pee 21 күн бұрын
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 21 күн бұрын
@@chris-pee And so C# 13 brings us back to think in assembly again :D
@izzyblackout1090
@izzyblackout1090 21 күн бұрын
How does it handles out of bound cases?
@mindstyler
@mindstyler 21 күн бұрын
Indexoutofboundsexception like always if course
@xeroxeroxeroxeroxeroxeroxero
@xeroxeroxeroxeroxeroxeroxero 19 күн бұрын
That was a smooth transition to the LINQ course advert. I'm not even mad
@davidpine7
@davidpine7 21 күн бұрын
Random array of numbers... [ 6, 9, 4, 20 ]. 🤣
@devincarlson8866
@devincarlson8866 21 күн бұрын
You’re finally using the new UI I see
@nickchapsas
@nickchapsas 21 күн бұрын
I couldn't take the peer pressure
@keyser456
@keyser456 21 күн бұрын
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.
@F1nalspace
@F1nalspace 20 күн бұрын
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.
@EdKolis
@EdKolis 21 күн бұрын
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 20 күн бұрын
The "^" operator is called a "Caret" in some countries.
@alexclark6777
@alexclark6777 21 күн бұрын
Thank goodness they're working on these useful features instead of niche requests like discriminated unions. /s
@ralfwarnat9257
@ralfwarnat9257 21 күн бұрын
is that related to criminal records?
@alexclark6777
@alexclark6777 20 күн бұрын
@@ralfwarnat9257 No, those guys are in a protected class.
@Thial92
@Thial92 17 күн бұрын
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 21 күн бұрын
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 21 күн бұрын
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 11 күн бұрын
Sad that ObservableCollection is not indexable.
@DoctorMGL
@DoctorMGL 8 күн бұрын
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 21 күн бұрын
c# having an identity crisis
@thelobo581
@thelobo581 15 күн бұрын
.net 8 is not a property of the solution though..😏
@antonmartyniuk
@antonmartyniuk 21 күн бұрын
Discriminated unions, please in C# 13
@user-zk5ym9ut1j
@user-zk5ym9ut1j 21 күн бұрын
Language team already responded like million times that this feature is not coming any time soon because of existing type system limitations
@diadetediotedio6918
@diadetediotedio6918 21 күн бұрын
​@@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 21 күн бұрын
Still no discriminated unions...
@johncarew1130
@johncarew1130 21 күн бұрын
Pretty sure that's known as a caret character, and not hat character.
@mindstyler
@mindstyler 20 күн бұрын
Is widely known as the hat character
@johncarew1130
@johncarew1130 7 күн бұрын
@@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 7 күн бұрын
@@johncarew1130 across programming it is almost always referred to as the hat character
@mindstyler
@mindstyler 7 күн бұрын
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 21 күн бұрын
If anyone ever uses this feature they better have a DAMN good reason.
@triGataro
@triGataro 21 күн бұрын
so Python #?
@YahushuaMessiah777
@YahushuaMessiah777 21 күн бұрын
Let me tell how often I use arrays like this..............um, I don't! I use Lists and fill them with Linq.
@vin7524
@vin7524 21 күн бұрын
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)
@Rick104547
@Rick104547 21 күн бұрын
Quite a niche use case, would rather see something like discriminated unions.
@yupii1997
@yupii1997 20 күн бұрын
To me this seems like a developer at microsoft needed this for a very very specific case, thus the reason it exists
@fsharplove
@fsharplove 21 күн бұрын
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 21 күн бұрын
Exactly this. Maybe we should just be thankful there isn't a new way to check for null in the next release?
@Sqrlmasta
@Sqrlmasta 21 күн бұрын
F# is a stable .NET language that supports discriminated unions... what's wrong with that?
@justinth83
@justinth83 20 күн бұрын
@@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 21 күн бұрын
^1 instead of ^0 is ultimate confusing
@catsandwich1259
@catsandwich1259 21 күн бұрын
Just think of '^X' being 'Length - X' and it makes sense. It keeps it consistent with the previous method.
@diadetediotedio6918
@diadetediotedio6918 21 күн бұрын
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 21 күн бұрын
@@diadetediotedio6918 for loop from the beggining: for (int i = 0; i
@diadetediotedio6918
@diadetediotedio6918 21 күн бұрын
@@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 21 күн бұрын
@@diadetediotedio6918 IMHO it would be better with the exact same for loop, just add ^
@interrrp_with_three_rs
@interrrp_with_three_rs 21 күн бұрын
what the hell? they should have just used negative indices
@diadetediotedio6918
@diadetediotedio6918 21 күн бұрын
It is the same as negative indices, lol. They just are not using the minus sign.
@interrrp_with_three_rs
@interrrp_with_three_rs 20 күн бұрын
@@diadetediotedio6918 exactly, why not
@socar-pl
@socar-pl 18 күн бұрын
So it's "-1" from python, but as microsoft-usual less intuitive
@jackkendall6420
@jackkendall6420 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 21 күн бұрын
My feature was not added, C# sucks!! 😡😡
@user-oy2bp9bq5m
@user-oy2bp9bq5m 21 күн бұрын
Did they cancelled your favourite feature or just not implemented yet? What's its name?
@marikselazemaj3428
@marikselazemaj3428 21 күн бұрын
Is it DU the feature you want?
@LeMustache
@LeMustache 21 күн бұрын
I feel so called out
@burger_flipper
@burger_flipper 21 күн бұрын
So much hate for nothing
@viktorstojanovic9007
@viktorstojanovic9007 21 күн бұрын
This gotta be the most useless feature they ever added
@nithinb9671
@nithinb9671 21 күн бұрын
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 21 күн бұрын
The index ranging using hat has been in since C#8 though.
@shanehebert396
@shanehebert396 21 күн бұрын
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 21 күн бұрын
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 21 күн бұрын
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 21 күн бұрын
Clickbait, this seems useless, would have been interesting if you show what it’s good for
@Tony-dp1rl
@Tony-dp1rl 16 күн бұрын
What a pointless addition to the language.
@jeroboam4486
@jeroboam4486 21 күн бұрын
It makes the code less readable, soon it's gonna look like Brainfuck! Linq is fine.
@TheOnlyDominik
@TheOnlyDominik 21 күн бұрын
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 21 күн бұрын
But, they are doing that too. Is not that all new C# feature are like this, just a portion
@diadetediotedio6918
@diadetediotedio6918 21 күн бұрын
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 21 күн бұрын
@@diadetediotedio6918 I think you are stupid!
@TheOnlyDominik
@TheOnlyDominik 21 күн бұрын
@@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 20 күн бұрын
​@@TheOnlyDominik'learning' is a word it would help to look up
@alfflasymphonyx
@alfflasymphonyx 21 күн бұрын
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!
Getting Started with Event Sourcing in .NET
37:07
Nick Chapsas
Рет қаралды 20 М.
Master the Design of Functional Types in C#
17:53
Zoran Horvat
Рет қаралды 10 М.
Teenagers Show Kindness by Repairing Grandmother's Old Fence #shorts
00:37
Fabiosa Best Lifehacks
Рет қаралды 41 МЛН
Follow @karina-kola please 🙏🥺
00:21
Andrey Grechka
Рет қаралды 21 МЛН
Stupid man 👨😂
00:20
Nadir Show
Рет қаралды 29 МЛН
Conforto para a barriga de grávida 🤔💡
00:10
Polar em português
Рет қаралды 88 МЛН
Await Async Tasks Are Getting Awesome in .NET 9!
9:24
Nick Chapsas
Рет қаралды 82 М.
Stop Using the Worst Way to Loop Lists in .NET!
9:35
Nick Chapsas
Рет қаралды 42 М.
delegate in C# | How it Works
27:33
TechBuddy EN
Рет қаралды 427
Understanding B-Trees: The Data Structure Behind Modern Databases
12:39
We should use this amazing mechanism that's inside a grasshopper leg
19:19
zig is the future of programming. here's why.
9:34
Low Level Learning
Рет қаралды 150 М.
"Stop Using Async Await in .NET to Save Threads" | Code Cop #018
14:05
Swagger is Going Away in .NET 9!
10:48
Nick Chapsas
Рет қаралды 62 М.
Why You Shouldn't Nest Your Code
8:30
CodeAesthetic
Рет қаралды 2,5 МЛН
5 Signs of an Inexperienced Self-Taught Developer (and how to fix)
8:40
Teenagers Show Kindness by Repairing Grandmother's Old Fence #shorts
00:37
Fabiosa Best Lifehacks
Рет қаралды 41 МЛН