How is the using() pattern useful? What is IDisposable? How
does it support deterministic finalization?



How is the using() pattern useful? What is IDisposable? How does it support deterministic finalizat..

Answer / chirantan

The using statement defines a scope at the end of which an
object will be disposed.

How to use
using (expression | type identifier = initializer) statement
where:

expression
An expression you want to call Dispose on upon exiting the
using statement.

type
The type of identifier.

identifier
The name, or identifier, of the type type. It is possible
to define more than one identifier of type type. Precede
each identifier = initializer with a comma.

initializer
An expression that creates an object.

statement
The embedded statement or statements to executed.

You create an instance in a using statement to ensure that
Dispose is called on the object when the using statement is
exited. A using statement can be exited either when the end
of the using statement is reached or if, for example, an
exception is thrown and control leaves the statement block
before the end of the statement.

The object you instantiate must implement the
System.IDisposable interface.

Example
// cs_using_statement.cs
// compile with /reference:System.Drawing.dll
using System.Drawing;
class a
{
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

}
}
Reference Link: http://msdn2.microsoft.com/en-
us/library/yh598w02(VS.71).aspx

Is This Answer Correct ?    5 Yes 0 No

Post New Answer

More OOPS Interview Questions

What is OOPS and How it is different from Procedural Programming ?

23 Answers   HP, Infosys, Thyrocare,


i ahve v low % in 12th n BSC which is aroun 50 coz science was imposed on me......nw m doin MCA n my aggregate in above 74%,what shud i say if asked about low previous percentage??????

4 Answers  


how to create thread in java?

17 Answers   IBM, Infosys, Wipro,


Difference ways of Polymorphism?

3 Answers  


Why we use classes in oop?

0 Answers  






What is difference between polymorphism and inheritance?

0 Answers  


What is nutshell in programming language?

1 Answers   Satyam, Tech Mahindra,


which are the 4 members functions in c++ objects that can either be declared explicitly by programmer or implementation if nt available.

4 Answers  


Write a java applet that computes and displays the squares of values between 25 and 1 inclusive and displays them in a TextArea box

0 Answers  


What is cohesion in oop?

0 Answers  


What is the correct syntax for inheritance? 1) class aclass : public superclass 2) class aclass inherit superclass 3) class aclass <-superclass

6 Answers   Wipro,


Why multiple inheritance is not possible?

0 Answers  


Categories