Click here to Skip to main content
15,868,111 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The last couple of weeks I have seen people using more and more the using statement. And honestly, I don't understand how this:

C#
public string DownloadPage(string URL)
        {
            /* Used to handle objects better. */
            using (WebClient Client = new WebClient())
            {
                Client.Encoding = Encoding.UTF8;
                return FixXML(Client.DownloadString(URL));
            }
        }


Is any different from:

C#
public string DownloadPage(string URL)
{
    /* Used to handle objects better. */
    WebClient Client = new WebClient()
    Client.Encoding = Encoding.UTF8;
    return FixXML(Client.DownloadString(URL));
}
Posted

Hi.. Go through this link Understanding the 'using' statement in C#[^]
 
Share this answer
 
When you use the using statement, you ensure that your variable will be properly disposed at the end of the procedure.

So the class of the variable you are using must implement the IDisposable interface.

Thus :

using (WebClient client = new WebClient())
{
   // ...
}


is strictly equivalent to :

WebClient client = new WebClient();
// ...
client.Dispose();


Hope this helps.

[EDIT]
Actually, the above is not quite correct:
It is strictly equivalent to:
C#
{ // Define the scope for client
  WebClient client;
  try
  {
    client = new WebClient();
    // ... (use of client)
  }
  finally
  {
    if (client != null)
      client.Dispose();
  }
}    // end of scope of client

[/EDIT]
 
Share this answer
 
v2
Comments
Yvar Birx 28-Feb-13 8:18am    
Thanks, clear and straight to the point.
Hi.Generally it is used for two purpose:

1-As a directive, when it is used to create an alias for a namespace or to import types defined in other namespaces.

2-As a statement, when it defines a scope at the end of which an object will be disposed.

GoodLuck
 
Share this answer
 
nections 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 can be exited either when the end of the using statement is reached or if an exception is thrown and control leaves the statement block before the end of the statement.
The object can be declared in the using statement, as shown above, or before the using statement, like this:
Font font2 = new Font("Arial", 10.0f);
using (font2)
{
// use font2
}


and visit this link


http://msdn.microsoft.com/en-us/library/yh598w02(v=vs.80).aspx[^]
 
Share this answer
 
Comments
Yvar Birx 28-Feb-13 8:16am    
So all it pretty much does is, it uses the Font, and then disposes it after the ending bracket?
Hi,
When outside of this your objects will be disposed.

If you don't use Dispose your code will be disposed at closing application time. Of late years, microsoft advice this.

for more information; [click here]
 
Share this answer
 
Comments
DaveyM69 28-Feb-13 16:36pm    
"If you don't use Dispose your code will be disposed at closing application time": or not. There can be occasions (especially but not exclusively when handling unmanaged resources with PInvoke etc) where it may not happen. Disposing of intensive or unmanaged resources in a timely manner is good coding practice - using 'using' makes this far more convenient and there is no excuse normally for not doing so.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900