C # You can use the using keyword to automatically clear object Resources.
# MyObject. cs
Using System;
Namespace MyProjects
{
Public class MyObject: IDisposable
{
Public MyObject ()
{
}
Public void Dispose ()
{
// Dispose
Console. WriteLine ("Disposed ");
//...
}
}
}
# Class1.cs
Using System;
Namespace MyProjects
{
Public class Class1
{
Public Class1 ()
{
}
Public static void Main (string [] args)
{
Using (MyObject myObject = new MyObject ())
{
Console. WriteLine ("quit ");
}
}
}
}
Using will automatically call the Dispose method of MyObject.
If MyObject does not implement the IDispose interface, an error occurs during compilation: The type "MyProjects. MyObject" cannot be implicitly converted to "System. IDisposable"
This writing method can replace
Try
{//...}
Catch
{}
Finnaly
{MyObject. Dispose ();}
Author Blog: http://blog.csdn.net/0328/