6 INSANE Things You Didn't Know You Could Write in C#

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody I'm Nick in this video I'm going to show you six insane things you can do with C- shop that you probably didn't know where possible or legal cop but it really is and be very careful these are not best practices or things you should be doing but the very good things to know that cop can do and can support because you might be able to find a way to use them in something that does really work for your team and your code base now this is more of a fun video of things that I like experimenting with and I'm sure if you watch my videos you will enjoy this one and the last one that I'm going to show you in this video is actually insane if you like of content and you want to see more make sure you subscribe for more training check out my courses on doet train.com okay let me show you what I have here I'm going to start with the first one which will look very very odd and that's the following I'll go in my program.cs over here and what I want to do is I want to write something that points to this test.txt file then reads it as a string and print it in the console so what I'm going to do over here is I'm going to say our file path and I have this file path record over here which I'm going to show you in a second and what I'm going to do is I'm going to say new file path over here and then I'm going to just point to that path now in this case I'm only going to point to the solution level folder so I'm going to say this because I want to continue with my weird insane technique so what I'm going to say is that actually this is the base path not the file path and this is the file path over here and now the file path is the base path plus the name of my project so how would you do that well for slash right so for Slash and then well for Slash and then the name of the file I want to read so test.txt of course and that is legal cop and I can go here and I can say console. WR line file. read all text point to the file path and then get the path and print it and in the console you're going to see Hello it did successfully read the file so how did we do that well as you might have imagined this file path is a bit special because it has an overloaded operator the division operator which happens to be the one that splits paths as well so we can split the path in a way in code I know it's a bit nuts but I've also seen people overload the pipe operator as well and the context in which they do actually does make sense but this would allow us to do something like this for example and this is also legal cop and this would also work the way I've seen this is in the CLI rap library to chain multiple commands together and pipe them one into the other I think it's pretty cool and for the love of God don't use this but it is pretty interesting that you can do that and you can reuse things that we've been known to use for a certain way and reuse them in a different way that still sort of makes sense by the way if you want to grab the code for any of these examples the code is in the description down below now before I move on I'd like to let you know we just launched a brand new course on Dome train call from Zero to Hero refactoring for cop developers in that course niiko sentino a principal software engineering manager in Microsoft is going to teach you everything you need to know from the very Basics on some pretty advanced stuff on how to refactor your cop code you're going to understand not only the how but also the why we make the decisions we make and at the end of this course you will know how to refactor and write better cop code Nick has been working at Microsoft for the past three years and he has really seen some good and some bad things so he really knows what he's talking about about and he poured his heart into this course now to celebrate the launch of this course I'd like to offer the first 500 of you at 20% discount code so use the link in the description and use discount code refa 20 at checkout to claim it these do tend to go very quickly so use it sooner L than later now back to the video the next one I've shown many many times but it doesn't get old so let's go ahead and say console. right line time provider. system.gc now and do that twice and let's say I want to wait for 2 seconds between these two calls how would I do that well well you might say traditionally that you can say task. delay for 2 seconds and certainly if I do that and I run my code I will have first the current time then a 2C delay and then the next time but what if I just say a wait two I can do that I can go ahead and do that and say run this and if I do that I'm going to have a delay for 2 seconds as you can see over here how the hell was that possible well if you watched any of my past videos you might have seen that there's actually the ability to await anything in C and the way this works is by doing something like this all you need to do is add the get a waiter extension method on the type you want to await and then as long as that thing returns a task awaiter which some of them you can reuse for example I'm reusing the task delay but some of them you can write on your own then anything can be awaited in any way now for the love of God don't do this because the problem with this is it doesn't really explain exactly what two is supposed to represent I've seen people do two seconds for example and make extension methods yeah that's more acceptable but also it's a bit weird so maybe don't do it but it is there so if you can think of something that could have a customer a waiter to become awaitable well it's for you to take and use however you want now let's say I want to iterate from 0 to five what I can do in C is I can say VAR zero here and then as as long as I is less than five then I ++ or less or equal than five actually because I said go all the way to five and if I do that I can say console. right line I I can right line the I and I'm going to see from zero to five in the console and that is fine but I don't think it's as cool as doing something like this where you can say for each for example and I'm going to say for h i in five and if I simply do that I'm going to just delete my for Loop just have my for each Loop and then print that in the console and I have the exact same thing in fact if I want to go from 5 to 10 and enumerate over that I can do something like this and say from 5 to 10 go ahead loop around and I'm going to have that over here how the hell is that legal C shop you might be asking well let me show you the way this is Legal C shop is by having these two classes over here the custom int enumerator which will be reused by these extension methods so as long as a class or a stuct has a current integer pointer and then the move next Boolean you can have a custom enumerator and what I'm doing over here is I'm using the range to enumerate a range of integers but I can also have the extension method on the integer itself and it doesn't have to extend anything or Implement any interfaces because as long as it matches the type through duct typing it will be acceptable so as you can see over here this just works again one of those things that you want to be very careful with but this means you can add the enumeration capability to any type not just integers or range if you have something that you think should be able to be enumerated you can do that an example would be that let's say you have a a directory object where you have a directory and you want to enumerate all the files in directory what you could do you just for each items in directory and as long as you have an enumerator for the directory this would work I think it's a pretty cool thing that you can do this and you can get very creative to think of ways to make good use of this now the next one I call extension nonsense which I've seen quite a lot actually in my life but let's say you want to represent today's date in a way that you think is fluent and makes sense what I've seen people try to do is add extensions on things that shouldn't have the given extension and then wrap logic around that for example today is the 30th of November 2023 so if I want to represent today in that way I might be tempted to do 30 of November 2023 and then if I go and I print that then as you're going to see this will generate behind the scenes today's date 30th of November 2023 don't do this don't add especially in things as fundamental as integers extension method that shouldn't really be there imagine that I went ahead and I made an extension method per year to do something like this it reads nice on the surface level and it looks smart I can see how especially a junior developer would think this is smart but be very careful with your extension methods on types especially that you don't own and the BCL types that they' used widely and they're not supposed to have this sort of thing now the next one is very interesting I recently found out about this red Twitter post and that post was from Sergey tacov I'm so sorry if I butchered your name but what Sergey showed is that let's say I want to have an object over here and I want to lock it to make sure that no other thread goes through that object so I have a thread safe block and you could do this by saying lock the object itself and then have some magic let's say console. right Line This was locked and thread save for example and if I go ahead and I run this this will just work you won't really see anything about the lock it itself but what s is showed is that you can go ahead and create a monitor class you can change the name space to system. threading which is exactly where the native monitor class lives in C and you can add a couple of methods the first one is the enter static method and also the exit static method and then the object is the object that's being locked and then the lock taken is the lock taken on that object this means that you can do something like this and say entering lock over here and exit loog over here and if you just do that and this file exists in your assembly if I just run the exact same code again Watch What Happens now the code will detect monitor class will go through the enter method first and then to the exit method and you're going to see all that printed in the console I don't know why or how and I'm not very comfortable that anyone can just make any type like this and potentially change the behavior but it's something you can do and it's pretty nuts I'd really like to know if you can think of a way to make a good use of this because I'm sure something is here but I don't know exactly what that is now the last one is actually nuts and it's so nuts the compiler of my ID won't even recognize it as legal cop but it is legal cop and it will compile so let's just to prove a point say console. hello world over here just right line and then behind that what I'm going to make is an async async async of async with async which awaits an async this is valid cop if I go and I just build this it works build succeeded with warnings but it did work it does run it prints hello world this is not illegal cop in fact I can chain a few ayns here so I can say async the async with async of array async with some async over here and if I do that this is also Legal C shop this works how well with a lot of basically we created a class acing that uses the acing method Builder attribute of type Builder which uses a lot of sort of duck typing in a way to represent a customer waiters State machines and so on it's pretty insane I don't fully understand it but Jared Parsons who is working in the compell team first showcase this and I think Fred who's also working I think for the net team showcased how you can do this array thing and it's kind of nuts and I'm scared why is this legal cop I don't know but it is and now you have this information again if you want to grab any of that code it is in the description down below but now I want to know from you what's the most insane thing you've done with cop leave a comment down below and let me know well that's all I had for you for thank you very much for watching and as always keep coding
Info
Channel: Nick Chapsas
Views: 48,797
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, dotnet, .net, c#, 6 INSANE Things You Didn't Know You Can Write in C#, 6 INSANE Things You Didn't Know You Could Write in C#
Id: lr1GtiF05EM
Channel Id: undefined
Length: 12min 26sec (746 seconds)
Published: Mon Dec 04 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.