Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2006

Tip: Looking for answers? Try searching our database.

about the using statement

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
tony - 24 Aug 2006 07:07 GMT
Hello!
Here I have two different Main called version 1 and version 2. Version 1 use
the using statement and
version 2 use local block which is {}

I wonder if version 1 and version 2 is functionally the same?
I mean it just a question of teste which one to use.

(Version 1)
public static void Main()
{
         using (Font MyFont = new Font("Arial", 10.0f), MyFont2 = new
Font("Arial", 10.0f))
         {
              // use MyFont and MyFont2
         }       // compiler will call Dispose on MyFont and MyFont2

         Font MyFont3 = new Font("Arial", 10.0f);
         using (MyFont3)
         {
                // use MyFont3
         }   // compiler will call Dispose on MyFont3
}

(Version 2)
public static void Main()
{
   {
        Font MyFont = new Font("Arial", 10.0f), MyFont2 = new Font("Arial",
10.0f)
         // use MyFont and MyFont2
   }

   {
        Font MyFont3 = new Font("Arial", 10.0f);
        // use MyFont3
   }
}

//Tony
Jon Skeet [C# MVP] - 24 Aug 2006 07:55 GMT
> Here I have two different Main called version 1 and version 2. Version 1 use
> the using statement and
> version 2 use local block which is {}
>
> I wonder if version 1 and version 2 is functionally the same?

Absolutely not. Your first version calls Dispose on the font after
using it. The second version doesn't, which means the font won't be
disposed until its finalizer is called (whereupon the finalizer will
call Dispose). That could happen a *long* time after the font has
finished being used.

> I mean it just a question of teste which one to use.

If you "own" a resource which ought to be tidied up (graphics
resources, IO resources, database resources etc) you should use the
using statement to achieve deterministic resource collection.

Jon

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.