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 are the features of oop?

0 Answers  


Question: Implement a base class Appointment and derived classes Onetime, Daily, Weekly, and Monthly. An appointment has a description (for example, “see the dentist”) and a date and time. Write a virtual function occurs_on(int year, int month, int day) that checks whether the appointment occurs on that date. For example, for a monthly appointment, you must check whether the day of the month matches. Then fill a vector of Appointment* with a mixture of appointments. Have the user enter a date and print out all appointments that happen on that date.

0 Answers  


what is the difference between ERROR and EXCEPTION?

14 Answers   NIIT, nvidia,


what is diff between .net 1.1 and .net 2.0

4 Answers  


What is class and object with example?

0 Answers  


Templates mean

0 Answers  


How can we use the preprocessor #if and #elseif.

2 Answers  


IS IT NECESSARY TO INITIALIZE VARIABLE? WHAT IF THE INSTANCE VARIABLE IS DECLARED final ? IS IT NECESSARY TO INITIALIZE THE final VARIABLE AT THE TIME OF THEIR DECLARATION?

0 Answers  


What is cohesion in oop?

0 Answers  


How to Increment the value of the empid E001 for each and every employee by using the programe?

1 Answers   Accenture,


Get me an image implementation program.

0 Answers  


WHAT IS THE ACTUAL DEFINATION OF OBJECT AND THE CLASS IN ONE SINGLE LINE WHICH THE INTERVIEWER WANT TO LISTEN.

11 Answers  


Categories