This is your first visit inside The Matrix. Welcome!

The Matrix has you…

This place will take you into the mirage of learning C# programming language, without any previous coding experience being required

Follow the White Rabbit…█

Close
Monday, August 20, 2018 19:19

The Using directive

.NET provides a simplified and easier way of working with resources that needs to be disposed (released when we no longer need them) through construct called a Using directive. Here is an example of how we can read a filename (just like in our previous two lessons) and automatically release it when we are done dealing with it, using this keyword:

By enclosing the declaration of our reader variable inside the using instruction, we can use it just as we normally would, but we don’t need to worry about releasing the file when we are done with it. However, if we supply a file that does not exist, we will still get a System.IO.FileNotFoundException, and that’s why we still need to wrap the Using directive in a Try Catch construct:

 

Comments

comments

Tags: , ,

Leave a Reply