The following sample shows how a user-defined class can implement its own Dispose behavior. Note that your type must inherit from IDisposable
C#, through the .NET Framework common language runtime (CLR), automatically releases the memory used to store objects that are no longer required. The release of memory is non-deterministic; memory is released whenever the CLR decides to perform garbage collection. However, it is usually best to release limited resources such as file handles and network connections as quickly as possible.
The using statement allows the programmer to specify when objects that use resources should release them. The object provided to the using statement must implement the IDisposable interface. This interface provides the Dispose method, which should release the object's resources.
A using statement
The following sample shows how a user-defined class can implement its own Dispose behavior. Note that your type must inherit from IDisposable.
using System;
class MyClass : IDisposable
{
public void UseLimitedResource()
{
Console.WriteLine("Using Momory");
}
void IDisposable.Dispose()
{
Console.WriteLine("Disposing Memory");
}
}
class Program
{
static void Main()
{
using (C c = new C())
{
c.UseLimitedResource();
}
Console.WriteLine("Now outside using statement.");
Console.ReadLine();
}
}
hi
ReplyDelete