// Create a disposable object and call Dispose()
// to free any internal resources.
MyResourceWrapper rw = new MyResourceWrapper();
rw.Dispose();
Console.ReadLine();
}
}
Of course, before you attempt to call Dispose() on an object, you will want to ensure the type
supports the IDisposable interface. While you will typically know which base class library types
implement IDisposable by consulting the .NET Framework 3.5 SDK documentation, a program-
matic check can be accomplished using the is or as keywords discussed in Chapter 6:
public class Program
{
static void Main()
{
Console.WriteLine("***** Fun with Dispose *****\n");
MyResourceWrapper rw = new MyResourceWrapper();
if (rw is IDisposable)
rw.Dispose();
Console.ReadLine();
}
}
This example exposes yet another rule of working with garbage-collected types.
Do'stlaringiz bilan baham: |