Built-In Interfaces: IDisposable |
|
Introduction |
Most objects that are used in an application consume memory and other resources. Some objects use more resources than others. Examples are objects used to establish connections from one computer to another, objects used to read from or write to a medium (hard drive, CD drive, USB drive, etc), etc. Whenever you have finished using such an object, you should (mostly must) make sure you free the resources it was using so the operating system can make those resources available to other applications of the same computer. |
To assist you with this, the .NET Framework provides an interface named IDisposable. Most of the time, you will not need to implement this interface because most of the classes that are resource-intensive have already been created and you will just need to use them. Still, if you want to, you should know that this interface exists. There are two main ways you will use the IDisposable interface and both ways do this indirectly. One way is that, if you have to, that is, if you judge it necessary, you can create a class that implements the IDisposable interface. The IDisposable interface is defined in the System namespace that is a member of the mscorlib.dll library. This means that you don't have to import any library to use it. The IDisposable interface contains only one method, named Dispose. Its syntax is: |
void Dispose(); As you can see, this method has nothing miraculous. When implementing the interface, use this method to free the resources a variable of the class was using. After calling this method, remember that the destructor of a class is always called when the variable gets out of scope. For this reason, it is a good idea to create a destructor for the class and call the Dispose() method from it. Here is an example: using System; public class Book { public int numberOfPages; public void Read() { } } public class Student : IDisposable { public Book text; public Student() { text = new Book(); } public void Dispose() { Console.WriteLine("The school is over - Get it of the book"); } ~Student() { Dispose(); } } public class Exercise { public static int Main() { Student std1 = new Student(); return 0; } } This would produce: The school is over - Get it of the book Press any key to continue . . . The second way you can use the IDisposable interface is the most important to us. To support the ability to release the resources that an object was using, the C# language provides an operator named using. Its formula is: using(Parameter) { } As you can see, this operator uses the formula of a function. It is equipped with parentheses and a body delimited by curly brackets. In the parentheses, declare the variable that will use the resources you are concerned with. Inside the brackets, use the variable anyway you want and that is appropriate. When the compiler reaches the closing curly bracket, it does what is necessary. For example, the compiler may have to close a connection that was used or to delete the object that was consuming the resources. Here is an example of using using: using System; public class Book { public int numberOfPages; public void Read() { } } public class Student : IDisposable { public Book text; public Student() { text = new Book(); } public void Buy() { Console.WriteLine("It's time to purchase a book"); } public void Dispose() { Console.WriteLine("The school is over - Get it of the book"); } ~Student() { Dispose(); } } public class Exercise { public static int Main() { using (Student std = new Student()) { std.Buy(); } return 0; } } This would produce: It's time to purchase a book The school is over - Get it of the book The school is over - Get it of the book Press any key to continue . . . |
|
||
Home | Copyright © 2010-2016, FunctionX | |
|