Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I came across a pseudo code yesterday :

string s;
using(new ClassA("hello"))
{
for(int i =0 ; i < some_count; i++)
s = "a";
s=null;
}



Class ClassA(string data) : IDisposable
{

public void Dispose()
{
/////do some disposal

}
}


can someone please explain me the significance of Using keyword here?


Thanks and Regards
Posted
Comments
Rahul VB 3-Jan-14 1:38am    
- and one more thing to add up: i also want to know that an object has been garbage collected, like i wrote above "s = null", i want to know that s has been garbage collected, how to achieve that?

Thanks

CP has a great article base to help with questions like these. Take a look at Understanding the 'using' statement in C#[^] article.
 
Share this answer
 
 
Share this answer
 
Please refer the following links :
Link1[^]
Link2[^]
Link3[^]
 
Share this answer
 
The reason for the "using" statement is to ensure that the object is always disposed correctly, and it doesn't require explicit code to ensure that this happens.When we use using in our code, no need to dispose the object of class.
 
Share this answer
 
Comments
Rahul VB 3-Jan-14 2:53am    
Hello Mam,
If i add a code:

static void Main(string[] args)
{
Class1 c;
using (c = new Class1())
{
c.hello();

}

Console.ReadLine();

}


class Class1:IDisposable
{
public void hello()
{
Console.WriteLine("hello");

}

public void Dispose()
{

Console.WriteLine("disposed");
}


}


The output is :
hello
dispose

My doubt is that i have not called the Dispose() explicitly, but CLR still executes it. If i would have not implemented the IDisposable interface explicitly then CLR would have done it automatically, because it is the job of the CLR.

Is this true for all standard Interfaces?

Thanks and Regards
Gitanjali Singh 3-Jan-14 3:16am    
As per http://www.codeproject.com/KB/cs/tinguusingstatement.aspx, the .NET CLR converts

using (MyResource myRes = new MyResource())
{
myRes.DoSomething();
}
to

{ // limits scope of myRes
MyResource myRes= new MyResource();
try
{
myRes.DoSomething();
}
finally
{
// Check for a null resource.
if (myRes!= null)
// Call the object's Dispose method.
((IDisposable)myRes).Dispose();
}
}


Note that it's not necessarily a matter of the object being disposed correctly, but more of whether it is disposed in a timely manner. Objects implementing IDisposable which hold on to unmanaged resources like streams and file handles will also implement a finalizer that will ensure that Dispose is called during garbage collection. The problem is that GC might not happen for a relatively long time. using makes sure that Dispose is called once you're through with objects.
Rahul VB 3-Jan-14 4:29am    
Hello Mam,
Thanks, very interesting.
-Rahul
Gitanjali Singh 3-Jan-14 6:08am    
If it helps you ,accept it as solution :)
The using block helps manage resources. Conceptually it protects the whole system's resources by specifying the scope of the usage of the resource. The using statement is combined with a type that implements IDisposable.

refer below links:-
This[^]
This[^]
 
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