Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am an occasional C# coder. I have just discovered a .NET4 syntax called the USING BLOCK so for example the code below works, BUT I don't know what the using BLOCKS are for. Googling isn't proving productive, so guys a quick clear explanation or link please.

Example:
   using (
   MailMessage message = new MailMessage
   {
       To = { new MailAddress("you@yourdomain.com", "you") },
       Sender = new MailAddress("robot@mydomain.com", "my auto sender"),
       From = new MailAddress("robot@mydomain.com", "same auto sender"),
       Subject = ".net Testing",
       Body = "Testing .net emailing",
       IsBodyHtml = true,
   }
)
   {
       using (
          SmtpClient smtp = new SmtpClient
          {
              Host = "smtp.office365.com",
              Port = 587,
              Credentials = new System.Net.NetworkCredential("robot@mydomain.com", "IncrediblySecurePassword"),
              EnableSsl = true
          }
       )
       {
           try { smtp.Send(message); }
           catch (Exception excp)
           {
               Console.Write(excp.Message);
               Console.ReadKey();
           }
       }
   }
(this is a corrected version of code from ShaneLS at StackOverFlow)

For the sake of anyone wanting to use this code the USING DIRECTIVE you need to add is this
using System.Net.Mail;


What I have tried:

I have tried googling, can't find the right combination of key words to get anything useful other than it comes in with .NET4
Posted
Updated 20-Feb-20 22:54pm

Some items are scarce resources, they should be properly handled and as soon as they are no longer needed they should be told to "close everything you opened down, and throw yourself away". Generally, these object implement IDisposable: and you can call Dispose on them to activate a destructor to close them down and release all resources.

The using block helps with this: it does two very handy things.
1) When the code lease the using block by any means - i.e. by normal means such as dropping to the line after the block, returning from the method, or by means of an Exception being thrown, the system will automatically call Dispose for you - It's like an implicit finally block of a try, if you like.

2) The scope of the item declared in the using block is limited to that block, it cannot be used outside as it no longer exists and cannot be referenced.

C#
using (MyClass ImOKInsideTheBlock = new MyClass())
   {
   ImOKInsideTheBlock.DoSomething(); // Fine.
   } // ImOKInsideTheBlock Disposed here.
ImOKInsideTheBlock.DoSomething(); // Compiler error as ImOKInsideTheBlock is no longer in scope.
 
Share this answer
 
 
Share this answer
 
 
Share this answer
 

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