Tuesday, April 16, 2024 04:31

Table of contents >> Debugging And Error Handling > The Using directive

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:


Tags: , ,

Leave a Reply

Follow the white rabbit