Use of “using”

Today I came across a question which ask where we use the keyword “using” in c#.
1. To import and refer another library or namespace inside a class
2. To safely initiate and dispose objects. Using is taking care of the dispose part which you normally do inside finally block of after try/catch. This simplify resource management and can be only used with types that implement IDisposable which has Dispose() method.
Eg:
using(Stream input = File.OpenRead(filename){}

Advertisements

Author: Harshani Nawarathna

Sri Lankan woman in Computing

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s