The previous generation example shows if you call a type of Dispose or Close method. If you decide to explicitly call one of these two methods, it is strongly recommended that they be placed in an exception-handling finally block. This ensures that the cleanup code is executed, so the previous code example can be modified to be a better form:
static void Main (string[] args)
{
byte[] Bytewrite = new byte[] {1, 2, 3, 4, 5};
FileStream fs = new FileStream ("Temp.dat", FileMode.Create);
Try
{
Fs. Write (bytewrite, 0, bytewrite.length);
}
Finally
{
if (fs! = NULL)
Fs. Dispose ();
}
File.delete (@ "d:\ user directory \ My Documents \visual Studio 2010\projects\consoleapplication1\consoleapplication1\bin\debug\ Temp.dat ");
}
Adding exception handling code is correct, and you should stick to it. Fortunately, C # provides a using statement, which provides a simplified syntax for getting the same results as the code above. The following shows how to rewrite the above code using C # 's using statement:
static void Main (string[] args)
{
byte[] Bytewrite = new byte[] {1, 2, 3, 4, 5};
using (FileStream fs = new FileStream ("Temp.dat", FileMode.Create))
{
Fs. Write (bytewrite, 0, bytewrite.length);
}
File.delete (@ "d:\ user directory \ My Documents \visual Studio 2010\projects\consoleapplication1\consoleapplication1\bin\debug\ Temp.dat ");
}
In the using statement, we initialize an object and save its reference to a variable. The variable is then accessed in the curly braces of the using statement. When compiling this code, the compiler automatically generates a try block and a finally block, in finally, the compiler generates code to transform the variable into a IDisposable and call the Dispose method. Obviously, the using statement can only be used for those types that implement the IDisposable interface.
NOTE: C # statements support the initialization of multiple variables as long as they are of the same type. In addition, the using statement allows only one initialized variable to be used.
The using statement can also be used to implement a value type of IDisposable. In this way, we can create a very efficient and useful mechanism to encapsulate the code required to start and end an operation. For example, suppose you want to use a mutex object to lock a block of code. The mutex class does implement the IDisposable interface, but calling the Dispose method on it only frees the local resources and does no processing on the lock itself. To simplify locking and unlocking a mutex operation, you can define a value type to encapsulate the locking and unlocking operations of the mutex object. The following mutexlock structure is an example, and then the main method demonstrates how to use the Mutexlock structure efficiently.
Class Program
{
static void Main (string[] args)
{
Mutex m = new Mutex ();
using (New Mutexlock (m))
{
}
}
}
struct mutexlock:idisposable
{
Private ReadOnly Mutex M_mutex;
Public Mutexlock (Mutex m)
{
M_mutex = m;
M_mutex. WaitOne ();
}
public void Dispose ()
{
M_mutex. ReleaseMutex ();
}
}
Section eighth using statements for C #