C# Using - Explained for dummies (and Pros)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to tutorial CU my name is Yannick and in this video we're going to talk about the using keyword that you can see right here [Music] well the c-sharp using keyword is used to manage resources and ensure that they are properly disposed of when they are no longer needed so here we are declaring a scope for file stream for example now here we have some data we take the bytes and then rewrite everything to that file once we're exiting this code block the file stream will get closed and disposed so the allocated memory will get released now we can use it for file streams we could also use it for database connections and other resources that require careful management so for example if a file stream or database connection would not get this post correctly chances are that we create a memory leak inside of our application network will slowly consume all the memory until our application will well get stuck now I'm pretty sure that you have seen such a using statement pretty often already but now I want to show you what's happening under the hood so let me just replace the code with another one I will just comment that out and bring that one in so here we have another filed stream right it's just a variable inside of our main method then we have a try block guess you know that from the try catching right so we're trying to execute code and if an exception occurs for example then we could catch it but here we only have two blocks a try block and a finally block so we're trying to write something to the five stream and once we're done with it we will jump into the finale block and we will dispose the object so please notice that we are calling a method right here which is called this post we will need that knowledge just for later on in this video now the good thing about a using statement is that it doesn't really matter of an exception of course so if we would not be able to write to that file stream in both scenarios with that code block or even with that one so if an exception occurs it doesn't matter because we are wrapped inside of a try so we're still trying it we are ignoring the exceptions but the finally block will get called anyway so if we are facing an exception or not doesn't really matter in any way the filed stream will get this post so this is exactly the same code as you can see it right here but this one here helps you understanding what's happening under the hood inside of the using statement if you're looking for way to massively boost your c-sharp skills please go ahead and check out our C sharp progress Academy it is a self-paced online course that teaches you asp.net angular unit testing and even c-sharp software design patterns we offer a 14-day money-back guarantee and I'm absolutely sure and confident to say that this is the fastest way on how you can progress as a c-sharp developer so you can find the link in the description below or popping up right now at the top right corner and if you like this video and if you have learned something new make sure to subscribe to our channel so that you are no longer missing any upcoming.net and C sharp and asp.net related videos right so smash the like button and the Subscribe button right now okay so there's even another syntax on how we can create using statements let me just replace that again there we go what we can do is we can create a using in a single line of coaches like that we go ahead and say using VAR file stream equals to new file stream and inside here we simply say example dot txt or whatever and then the file mode open so now we have that written in a different syntax right but I want to show you that I'm not the fan of that too much because if I just copy it over here and if we now compare them please just ignore the errors you can see that this first version here the original one right that this is really giving us a nice view on the scope of the actual using why that one here is just well you cannot really see that we are using a using statement right here right so this one definitely shows us the scope immediately does not provide any curly braces so you can use the one you like but as I said I'm more the fan of the traditional one because it's easier to read and it gives you a nice and clear idea of the scope now do you remember when we called that dispose method once we had that try and finally block so let's just create our own object let's create our own connection let's say you want to create a custom some database connection or whatever anything where you want to make sure that it really gets disposed in the correct way in that way would create a class let's just call it my connection like this and now let's try to create our own using statement so let's create a using here let's say my connection let's say connection equals to new my connection and we have the curly braces and you can see that this is not working it's not working because if we use using we need to have a dispose method and we only get that if we just inherit our class so my connection from I disposable now in that way we have to because it's an interface we have to implement the dispose method as you can see right here inside here we could now handle all the stuff that we need to do to really make sure that the object gets closed and that all the allocated memory gets well released but now you can see that we are able to use our my connection our custom class inside of a using statement so once our own using statement right here would be done so once we reach line 19 just for example the dispose method of our my connection object would automatically get called and our object would get this post great and that's it for this video so thanks for watching if you like this video make sure to subscribe to our channel so that you no longer miss any upcoming videos and definitely give this video a thumb up and if you want to take your c-sharp skills to the next level check out our c-sharp progress Academy foreign [Music]
Info
Channel: tutorialsEU - C#
Views: 3,177
Rating: undefined out of 5
Keywords: c#, .net, c-sharp, csharp, programming, visual studio, c sharp, learn c#, c# programming, c# tutorial, c# for beginners, learn c# programming, c# course, tutorial, coding, .net core, c# using keyword, microsoft mvp, nick chapsas, keyword, dotnet, clean code, using keyword in c#, using c# keyword, using keyword, game development, software engineering, unity scripting tutorial, c sharp tutorial, core, how to code, development, .net interview questions, what is c#, dot net, C# Using
Id: Gfl0qD0gTyE
Channel Id: undefined
Length: 6min 8sec (368 seconds)
Published: Wed Mar 01 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.