.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:
1 2 3 4 |
using (StreamReader reader = new StreamReader(fileName)) { // Use the reader variable here } |
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.… Read more