Labels

Monday, March 9, 2009

'Using' Myth



Hi,

The C# programming language's using statement makes a call to the Dispose method More Automatic, by simplifying the code that you must write to create and clean up an object.

The using statement obtains one or more resources, executes the statements that you specify, and then disposes of the object.

Note: That the using statement is only useful for objects with a lifetime that does not extend beyond the method in which the objects are constructed.

Imp – Use of ‘Using ‘ block requires the Object to implement ‘IDisposable’ interface.

class Resource :IDisposable

{

public void Dispose()

{

// Write code

}

}

private void btn_UsingMyth_Click(object sender, EventArgs e)

{

#region Approach 1

Resource theInstance = null;

try

{

theInstance = new Resource();

}

finally

{

if (theInstance != null) theInstance.Dispose();

}


No comments:

Post a Comment