Skip to main content

This version of the product is no longer supported, and this documentation is no longer updated regularly. See the latest version of this content.Opens in a new tab

Using Statement

It is very important that resources associated with .NET proxy objects are released as soon as possible. Each open object has a set of resources associated with it on both the client and the server. For .NET, garbage collection is automatic, but non-deterministic. The program does not have control over when the garbage collector runs.

To explicitly free an object's resources, you can invoke its Dispose method. It is probably both safer and more convenient to use the C# using statement. With the using statement, you instantiate your object and then create a scope (curly braces) for it. When execution reaches the closing curly brace, the system automatically invokes Dispose on the object.

The following example uses the using statement to create a scope for a Contact object. When execution reaches the closing curly brace, the system severs the object's connection to the server and frees its resources.


try
{
 Provider.Contact contact;
 using (contact = Provider.Contact.OpenId(cnCache, "1"))
 {
   Console.WriteLine("Name: {0}", contact.Name);
   Console.WriteLine("Type: {0}", contact.ContactType);
 } 
 if (!contact.IsConnected)
 {
   Console.WriteLine("Contact disconnected");
 }
}
catch (CacheException e){}   

Note that in the example cnCache represents an open CacheConnection instance.

Note:

For more information on the using statement, see The Using Statement in the C# Language SpecificationOpens in a new tab.

FeedbackOpens in a new tab