6 C# keywords you (probably) never had to use

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody i'm nick and this i'm going to talk about six or even maybe seven keywords that you never have to use in c shop well probably the reason why i say you never had to use them and you'll see as we go is that they're very niche in terms of what they do i'm only gonna briefly explain each one of them because they can get very advanced the more you dive deeper into them but i think there is value in knowing about them at least that they exist and what they do on the very surface level if you want to of course go deeper on them you will always find documentation microsoft has written for those keywords specifically if you like the type of content and you want to see more make sure you subscribe and there's a notification bell to get alerted when i upload a new video so let's go straight into the code and what do we have here well i'm going to be running them through this program and the first thing i have is a combination of keywords the checked and unchecked keywords now let's just take a look at this let's say you have an unsigned integer and the value is the max u int value an unsigned integer is an integer that is only only consists of positive numbers so basically there's no maximum of 2 billion something because there's no negatives you have 4 billion something so that's the main idea now if i say this and print writeline and i just run this program let me just quickly change my program.cs and i run it and what i should see in the console is just the maximum numbers now what do you think will happen if i do this what would you expect the the code to do in runtime um you can leave a comment before you see it or you can just keep it in your head but now i'm gonna run it and let's see what the value is after the incrementation remember we set it to the max and now it's zero but why it's zero right well it's sort of like the is it called the tachometer the thing on your car that counts the kilometers or miles and once you exceed the maximum number it just loops around to the zero value this is basically what's happening and if you wanted to prevent that then you would use the checked keyword and if you use a checked keyword then once you run this code again let's see what happens it goes boom it says arithmetic operation result is an overflow so you have an overflow exception because you cannot increase over the maximum value and this feature i've only seen it actually when i was decompiling link code and i saw that some of the bits there were checked um it's very much niche but it's one of those things that like of all the words that we're gonna see in this video that's probably the one you should care about and know how to use because you might actually need to use it you know you might need to handle an exception when an overflow happens on one of your values and you need to deal with it and obviously unchecked will negate um the the checked this is not the only way you can use it by the way um you can even do things like this where you can have a checked scope here and you can wrap it in parenthesis and then that will also throw um but yeah you can see that you can nest a check and uncheck them by default it's unchecked and unchecked will negate the outer checked uh context and it will just increment it to zero again so out of all the ones we're to see that's probably the one you might actually have to use at some point if you're not already using it now the next one is the unsafe keyword what if i told you and for some of you this might be completely new thing that c sharp actually has pointers similar to how c and c plus plus does c sharp does in fact have pointers but it is also a managed language right we have the garbage collection automatically collects all the dangling things so why well because you might have do some very low level stuff and you might need to talk to addresses directly you probably never had to deal with unsafe code obviously unsafe code will be unmanaged but you can write it and let's see how we can do this first we need to mark either the method or the context so we can either say unsafe here or i could even say unsafe here in a block i'm gonna go with the method one and unsafe has to be enabled on the compiler level on the project level so i'm gonna allow rider to add it and for reference you can actually find that as a setting in your cs project it's the and allow unsafe blocks and once we do that let's say we have an integer which is called snoop dogg and the value is 420 because of course it is i typed noon that's more like a snoop here we go and now let's get a pointer to that integer's address and we can do that by saying in star pointer equals ampersand snoop dogg and now i got a pointer to the address of the variable this might look like magic to you but it actually works and let's let's take a look at this in fact before i print anything i'm going to just change this here in the program.cs and i'm going to take a breakpoint and show you what this looks like so we create the integer and now we have a pointer and you can see that the pointer has this hex looking value which is actually the address of where this thing is allocated now you can do quite a few things syntax wise here but a few that i find interesting is that you can just say the int value is and you can get the actual value from the pointer by adding the asterisk in front of it and saying asterix pointer you can also get the string representation value and say the string value is and say pointer and then this is valid c sharp uh i'm laughing because the first time i saw this i'm like another arrow function but no this is actually sort of representing a dot those of you familiar with c would think this is fine but if you just write c sharp this might be a bit weird but yes this will actually get the string value of the value of the property and the console.writeline the address value is and we can cast to an integer on the pointer and print those things and if i let this run you'll see that we have 420 from the pointer value the string from the tostring and then the actual address integer by the pointer itself would you have to deal with this realistically no really you probably never would have to but i think there's value in knowing about it and we're only gonna get weirder from here because we're gonna add on top of this feature with the next keyword which is the fixed keyword so like like i said the code that you write in a nun self is unsafe code but let's say that you have something like this a private string text here and it's my name so we have nick over here a class level reference type for example the string will be allocated on the heap but i might need to do something with this in unmatched code and really a string is just an array of characters i cannot however do this and say pointer equals ampersand text because i cannot guarantee that this reference to the string won't be relocated i'm confusing you i cannot guarantee that the reference that i'm getting of the address of this string won't be relocated during garbage collection i know it's weird but imagine that each character of this string is allocated in a single location and i need to do something with it and i need to guarantee that the garbage collection want to move it around how can i do that well that's where the fixed statement comes into play and fixed is another one of those things that is pretty unsafe and i can enable this if i say and save here of course and now i can deal with this text with knowing that it is pinned in the address its characters the characters of this string will be there and i can do stuff with them and here's an example i could say var name equals string dot empty and then i can get a pointer and then loop around that and say as long as this is not null then let's take that and append it to the name so name is the value of this character and then let's move to the next address because it's four characters next to each other and i want to move from one address to the other to read every character i know this is confusing all you need to know is fixed will pin the thing in the memory but i want to show you how it actually works as well and not just call a few things and call it today and say the name is name and now if i run this and i actually need to update the program.cs if i run this the name is nick and if we actually debug this code which i think you'll find more interesting let's take a break point here then you can see that the text is there now we have the pointer to the text we initialize the empty string and then we use that the value is not it you can actually see that it's the character n and we just append it by getting the value incrementing to the next address i and you can see the name being built i will update you later right there it's fine you can see the name being built and now we just print it so yeah fix we pin a reference type in where it's allocated so you can do something with it and not relocate it now the next one is a size of a keyword and you might have used this before but this falls again under the unsafe code stuff so you might not have used it but it's pretty straightforward you can use size of to get the size that a specific type we'll use when allocated so if i say size of a byte it will print let's just say this right line the size of a byte is size of byte and if i run this you'll see that the size of a byte is one however the size off can only be used with unmanaged types so you cannot use like i don't know even this size of example you cannot just use this you're gonna get an error saying you cannot take a manage type and actually print the size of it which means we can actually make a method called public static unsafe avoid display size of t but if i want to do that and let's just say the size of and here have the type so type of t is size of t then you actually get a seven key what you didn't know about probably for free we have to constrain this t and say where t is unmanaged and now we can actually use that which means i can just say display of or display size of byte i can display the size of let's say boolean or bool integer and so on and so forth so if i execute this we are getting the the size of those things another interesting thing is that if we had the struct so public struct let's say a point in space in 3d space so what does a point in 3d space have it has an x and y and a z then because the struct is an unmanaged type you can actually use sizeof on it so point goes here printing it and it's three doubles as you can see so eight bytes each 24 bytes and if i was to just show you the uh the double as well how much memory you allocate for that size of double is eight so that is another set of keywords that you probably never have to use because it's again very niche right why do you need to know in normal code you probably wouldn't you have to be doing very low level stuff to actually need to do anything with that and then last but not least we have the stack alloc example you might have seen a pattern here it's mostly around unsafe code and memory allocation this one is actually safe code so you don't have to worry about that but let's take a look at the stack alloc keyword so the stackable keyword has been around for a long time but now with spans it's actually quite more relevant and you can have something like that where you have a span of an integer and let's say you have some numbers and you create a stack a lock int array where you have three characters four two and zero now what does stackalope do well it's in the name it allocates the thing that you created in the stack instead of the heap and the question is why why would you do that 99.99999 this at the time you don't need to do that you shouldn't do that um but suckaluck comes with a lot of things that when it comes to performance like insane levels of performance like cpu cache hits matter level performance then you might want to consider if i was to use that spark the code would look something like this where i'm just um iterating over the array that i just created the span of integers and if i am to change it here and executed um and i run this with a break point here you'd see that we have the uh the array which is allocated using stacker lock and then iterate create the this which coincidentally sums up to 420 and it says the result was 420 with this array allocated in the stack here's the thing using stack a lock to allocate things will create less load on the garbage collector because stack a lock once it is out of scope it will automatically get disposed you you won't have to wait for the cabbage collection to find something and then just delete it because nothing refers to it it's also faster to allocate in general than a heap array i say in general because there's some nuances around it but i won't dive deeper into those because we're gonna go way way off now the good thing about it is that it's very secure because it has things like buffer overrun detection baked into it and once something goes over it it will actually automatically shut down so it's pretty safe to use it's very performant but it's very niche the thing about it is that it can greatly decrease the chances of getting a catch miss because of locality of data but the thing is if all the engineers that write c sharp were like 10 million then the number of them that would actually have to use this that they write code that needs this is very very slim and low i've seen in some cases where you have something like this where you just want to allocate a span of bytes what you theoretically could do and i can just say bytes here is you can say numbers.length less than 128 because it's small enough in fact equals to 128 and if that's the case then use stackalog to allocate a byte array for the length and if that's not the case then allocate a regular byte array however realistically you you shouldn't think about it this is more about knowing it exists and maybe benchmarking entertaining the idea of justifying using it but quite honestly i am not using it i'm not using any of those features but i think they're good to know about i totally understand that this is probably very advanced and very niche content but i find great joy in actually exploring those things because who knows when you might ever get to the point where you're like yeah i think i need to use takalok or whatever that's all i had for you for this video thank you very much for watching special thanks to my patrons for making videos possible if you want to support me as well you're going to find it in the description down below leave a like if you like this video subscribe for more under like this ring the bell as well and i'll see you in the next video keep coding you
Info
Channel: Nick Chapsas
Views: 43,597
Rating: undefined out of 5
Keywords: Elfocrash, elfo, coding, .netcore, dot net, core, C#, how to code, tutorial, development, software engineering, microsoft, microsoft mvp, .net core, nick chapsas, chapsas, clean code, pointers in c#, addressed in c#, pointers, stackalloc, malloc, garbage collection, gc, 6 C# keywords you (probably) never had to use, 6 c# keywords you never had to use, C# unused features, c# fixed, c# checked, c# unchecked, c# sizeof, dotnet, .net
Id: dySs0DONbdk
Channel Id: undefined
Length: 17min 33sec (1053 seconds)
Published: Mon Mar 22 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.