.NET Tip: Managing Resources with the Using Statement Boston MA

Take control of the lifetime of your objects. Release your object's resources on your schedule, not when the CLR determines they are no longer needed.

Local Companies

Eze Castle Integration
(617) 217-3000
1 Federal Street, 9th Flr.
Boston, MA
Cogent Communications
(617) 482-5546
1 Summer Street, Ste. 450
Boston, MA
WordStream Internet Marketing
617-457-7870
133 Federal St.
Boston, MA
Applied Technologies, Inc.
(617) 742-2525
One Center Plaza, Ste. 240
Boston, MA
Thomas Reuters
(617) 856-2000
22 Thomson Place
Boston, MA
Starr Center @ The Schepens Eye Research Instittute
(617) 912-0100
185 Cambridge Street, 2nd Floor
Boston, MA
VIPER consulting_inc
(617)6860170
660 Massachusetts Avenue, Suite 6
Boston, MA
JCALPRO
(617) 954-2345
415 Summer Street
Boston, MA
INX Inc.
(617) 728-4440
2 Oliver Street, 10th Floor
Boston, MA
Brattle Consulting Group, Inc.
(617) 229-7210
8 Faneuil Hall Marketplace
Boston, MA

provided by: 
Originally published at Internet.com


Last week, I showed you how you can help ensure that your objects release their resources by implementing IDisposable. This week, I want to show you the using statement that allows you to specify when your objects release their resources. Objects declared in the using statement will explicitly be disposed when the end of the using block is reached. I often use nested using statements for data access to precisely control the creation and disposal of my objects. Here is how you can take advantage of the using statement to create a database connection, create a command, and execute a data reader to access your data. using (SqlConnection CN = new SqlConnection("*** Your connection string here ***")) { CN.Open(); using (SqlCommand cmd = new SqlCommand("SELECT * FROM authors", CN)) { using (SqlDataReader dr = cmd.ExecuteReader()) { // Access elements items here } } }

By nesting the using statements above, you can be sure you are creating objects only when necessary and that, once everything is finished, your objects will all be disposed of in a timely manner. If an error should occur anywhere along the way, any objects created up to that point will still be disposed of when the using block is exited. The only caveat is that the object declared in the using statement must implement the IDisposable interface. Keep this in mind if you plan on taking advantage of the using statement with your own classes.

About the Author

Jay Miller is a Software Engineer with Electronic Tracking Systems, a company dedicated to robbery prevention, apprehension, and recovery based in Carrollton, Texas. Jay has been working with .NET since the release of the first beta and is co-author of Learn Microsoft Visual Basic.NET In a Weekend. Jay can be reached via email at jmiller@sm-ets.com.

Author: Jay Miller

Read article at Internet.com site

Featured Local Company

Eze Castle Integration

(617) 217-3000
1 Federal Street, 9th Flr.
Boston, MA

Related Local Events
Software Development Best Practices 2009
Dates: 9/21/2009 - 9/24/2009
Location: Hynes Convention Center
Boston, MA
View Details

EMBEDDED SYSTEMS CONFERENCE - BOSTON 2009
Dates: 9/21/2009 - 9/24/2009
Location: Hynes Convention Center
Boston, MA
View Details

September Networking Breakfast
Dates: 9/15/2009 - 9/15/2009
Location: Holiday Inn Boston - Somerville
Somerville, MA
View Details

September Networking Breakfast
Dates: 9/15/2009 - 9/15/2009
Location: Holiday Inn Boston - Somerville
Somerville, MA
View Details

LINUXWORLD SUMMIT 2009
Dates: 9/1/2009 - 9/1/2009
Location: IDG World Expo
Framingham, MA
View Details