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