this question is not related to using keyword which is used to import the class using namespace.
Be specific when u give me an answer bcoz it is an interview question.
Thanks
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Jaish MathewsPosted May 10, 2010, 12:58 PM
2 things
CrishPosted May 10, 2010, 12:45 PM
you can create any class file where u can make properties, methods etc.
once made this file you can use this class file publicly in asp.net files. so u need to add
"using classname" in this file for accessing its methods and properties.
Jaish MathewsPosted May 10, 2010, 12:22 PM
C#, through the .NET Framework common language runtime (CLR), automatically releases the memory used to store objects that are no longer required. The release of memory is non-deterministic; memory is released whenever the CLR decides to perform garbage collection. However, it is usually best to release limited resources such as file handles and network connections as quickly as possible.
The using statement allows the programmer to specify when objects that use resources should release them. The object provided to the using statement must implement the IDisposable interface. This interface provides the Dispose method, which should release the object's resources.
A using statement can be exited either when the end of the using statement is reached or if an exception is thrown and control leaves the statement block before the end of the statement.
In layman's language, we can ensure that the object will be released from memory automatically without any explicit work
Example -
Below FileStream object fs will be released once out of "using" block. We no need to do any thing
using (FileStream fs = new FileStream(@"c:\text.txt",FileMode.Create))
{
//fs has scope in this block
}
//fs released from memory. Now it's our of scope
Imp - Stream objects and any COM objects should be wra with "using" to ensure their release automatically after the scope
Custom using blocks
If you have a class which has code related to COM and the developer using your classs need to apply "using" statement, what to do. Just inherit your class from IDisposable
Eg-
My class definition
class JaishClass : IDisposable
{
//Contains some COM and Stream related codes
void IDisposable.Dispose()
{
Console.WriteLine("Disposing limited resource.");
}
}
Below developer using my class
using (JaishClass objJaish = new JaishClass())
{
c.UseLimitedResource();
}
//objJaish will be released after after "using" block
"using" block can use for any object which is inherited from "IDisposable".
Another interesting thing is that once you opened the comiled assembly in ildasm.exe, you can see CLR added some additional blocks by identifying the "using" block.