示例
Sample.cs
using System;
namespace ClassLibrary1
{
public class Sample : System.IDisposable
{
//constructor
public Sample()
{
Console.WriteLine("Database connected.");
}
//method
public void DisplayDataFromDatabase()
{
Console.WriteLine("Reading data from database");
}
//Dispose
public void Dispose()
{
Console.WriteLine("Database disconnected");
}
}
}
Program.cs
using System;
using ClassLibrary1;
namespace IDisposableExample
{
class Program
{
static void Main()
{
//create object using "using structure"
using (Sample s = new Sample())
{
s.DisplayDataFromDatabase();
}
Console.WriteLine("Some other work here");
Console.ReadKey();
}
}
}