Wednesday, October 28, 2009

Two uses of Using clause in C#

I recently came across a question in the interview.
What are two uses of "Using" clause in C# programming language.

Here is the answer.

  1. "using" at the top of the page is used to import namespaces into the current coding block.
  2. "using" is also used to dispose the object at the end of the current usage block. It is important to note that, "using" can only be used on the objects which have IDisposable interface implemented. For example,


using (SPweb myWeb = SPContext.Current.Web)
{
    //work with myWeb object here
}
Note: This example is using C# language for SharePoint

No comments:

Post a Comment