D
Duggi
I used to wonder why MS implemented C# to accept the following code
using (Font f = new Font())
{
// some code here.
}
While the same can be achieved through
{
Font f = new Font()
// some code here.
}
Just making code as a block and create the object inside the block.
Why MS took pain to implement the semantics to understand in C# it as
in first block.
As per my understanding the above code does the following things
1. Creates a block where f is used
2. When code block completes execution, f is garbage collected.
I was wrong. There is one major significant difference between the two
code blocks.
Actually, what ever the object used in the using(object) statement,
has to implement IDisposable. I think by now you got the difference.
The beauty of the using(object) statement is that after the execution
of the using block object.Dispose() will be called by the framework,
releasing all the unmanaged resources.
See below code:
class TestC : IDisposable
{
public void UseLimitedResource()
{
Console.WriteLine("Using limited resource...");
}
void IDisposable.Dispose()
{
// this class uses significant unmanaged resources and are relesed
here.
Console.WriteLine("Disposing limited resource.");
}
}
class Program
{
static void Main(string[] args)
{
using (TestC testC = new TestC())
{
testC.UseLimitedResource();
}
Console.ReadLine();
}
}
However I ran into another BIG doubt that why C# allows the following
code
class Program
{
static void Main(string[] args)
{
TestC testC = new TestC()
using (testC)
{
testC.UseLimitedResource();
}
testC.UseLimitedResource();
Console.ReadLine();
}
}
Object is already disposed, still you can use it, driving to CRASHing
your own applications????
I appriciate your help understanding it properly.
-Cnu
using (Font f = new Font())
{
// some code here.
}
While the same can be achieved through
{
Font f = new Font()
// some code here.
}
Just making code as a block and create the object inside the block.
Why MS took pain to implement the semantics to understand in C# it as
in first block.
As per my understanding the above code does the following things
1. Creates a block where f is used
2. When code block completes execution, f is garbage collected.
I was wrong. There is one major significant difference between the two
code blocks.
Actually, what ever the object used in the using(object) statement,
has to implement IDisposable. I think by now you got the difference.
The beauty of the using(object) statement is that after the execution
of the using block object.Dispose() will be called by the framework,
releasing all the unmanaged resources.
See below code:
class TestC : IDisposable
{
public void UseLimitedResource()
{
Console.WriteLine("Using limited resource...");
}
void IDisposable.Dispose()
{
// this class uses significant unmanaged resources and are relesed
here.
Console.WriteLine("Disposing limited resource.");
}
}
class Program
{
static void Main(string[] args)
{
using (TestC testC = new TestC())
{
testC.UseLimitedResource();
}
Console.ReadLine();
}
}
However I ran into another BIG doubt that why C# allows the following
code
class Program
{
static void Main(string[] args)
{
TestC testC = new TestC()
using (testC)
{
testC.UseLimitedResource();
}
testC.UseLimitedResource();
Console.ReadLine();
}
}
Object is already disposed, still you can use it, driving to CRASHing
your own applications????
I appriciate your help understanding it properly.
-Cnu