using System;namespace Text { public class Program { static void Main(string[] args) { using (TestClass test = new TestClass()) { } Console.ReadKey(); } } public class TestClass : IDisposable { public void Dispose() { Console.WriteLine("自动执行Dispose"); } } }
using System;namespace Text { public class Program { static void Main(string[] args) { TestClass test = new TestClass(); try { } finally { test.Dispose(); } Console.ReadKey(); } } public class TestClass : IDisposable { public void Dispose() { Console.WriteLine("人工执行Dispose"); } } }
{
public class Program
{ static void Main(string[] args)
{
using (TestClass test = new TestClass())
{
} Console.ReadKey();
}
} public class TestClass : IDisposable
{
public void Dispose()
{
Console.WriteLine("自动执行Dispose");
}
}
}
{
public class Program
{
static void Main(string[] args)
{
TestClass test = new TestClass();
try
{
}
finally
{
test.Dispose();
} Console.ReadKey();
}
} public class TestClass : IDisposable
{
public void Dispose()
{
Console.WriteLine("人工执行Dispose");
}
}
}