By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,200 Members | 1,665 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,200 IT Pros & Developers. It's quick & easy.

C# using statement

P: n/a
I have seen the "using" statement used in strange ways in some C# code.
Example:

using (StreamReader sr = new StreamReader("TestFile.txt"))
{
// do some stuff...
}

What effect does the using statement give you in this example?
Nov 17 '05 #1
Share this Question
Share on Google+
18 Replies


P: n/a
"Trevor" <tr****@spam.com> wrote in message
news:OK**************@TK2MSFTNGP10.phx.gbl...
I have seen the "using" statement used in strange ways in some C# code.
Example:

using (StreamReader sr = new StreamReader("TestFile.txt"))
{
// do some stuff...
}

What effect does the using statement give you in this example?


It explicity forces the stream reader object to be 'cleaned up' when leaving
the using block. So, you do not have to worry about closing the file, etc,
that is all handled for you.

Personally, I don't care for it. I'd rather just say

sr.Close();

and be done with it.

--
Adam Clauss
Nov 17 '05 #2

P: n/a
"Trevor" <tr****@spam.com> wrote in message
news:OK**************@TK2MSFTNGP10.phx.gbl...
I have seen the "using" statement used in strange ways in some C# code.
Example:

using (StreamReader sr = new StreamReader("TestFile.txt"))
{
// do some stuff...
}

What effect does the using statement give you in this example?


It explicity forces the stream reader object to be 'cleaned up' when leaving
the using block. So, you do not have to worry about closing the file, etc,
that is all handled for you.

Personally, I don't care for it. I'd rather just say

sr.Close();

and be done with it.

--
Adam Clauss
Nov 17 '05 #3

P: n/a
Trevor wrote:
I have seen the "using" statement used in strange ways in some C# code.
Example:

using (StreamReader sr = new StreamReader("TestFile.txt"))
{
// do some stuff...
}

What effect does the using statement give you in this example?

this:

using (SomeType varname = new SomeType())
{
// use varname in here
}

is functional equivalent to this:

SomeType varname = new SomeType();
try
{
// use varname in here
}
finally
{
((IDisposable)varname).Dispose();
}

SomeType needs to implement IDisposable in order to be used like this.
There's some additional rules about structs and boxing etc. that is in
work but the above code example should show what it does.

In plain english:
it makes sure the Dispose method of the IDisposable interface on that
object is called as execution is leaving the block.

--
Lasse Vågsæther Karlsen
http://www.vkarlsen.no/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Nov 17 '05 #4

P: n/a
Trevor wrote:
I have seen the "using" statement used in strange ways in some C# code.
Example:

using (StreamReader sr = new StreamReader("TestFile.txt"))
{
// do some stuff...
}

What effect does the using statement give you in this example?

this:

using (SomeType varname = new SomeType())
{
// use varname in here
}

is functional equivalent to this:

SomeType varname = new SomeType();
try
{
// use varname in here
}
finally
{
((IDisposable)varname).Dispose();
}

SomeType needs to implement IDisposable in order to be used like this.
There's some additional rules about structs and boxing etc. that is in
work but the above code example should show what it does.

In plain english:
it makes sure the Dispose method of the IDisposable interface on that
object is called as execution is leaving the block.

--
Lasse Vågsæther Karlsen
http://www.vkarlsen.no/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Nov 17 '05 #5

P: n/a
But what if an unhandled exception was raised during the

// do some stuff

bit of code. Does the using (StreamReader sr = new
StreamReader("TestFile.txt"))
code construct automatically call the Dispose method as part of the stack
walk. If so, then it is similar to a try..finally and therefore must add
some value.

"Adam Clauss" wrote:
"Trevor" <tr****@spam.com> wrote in message
news:OK**************@TK2MSFTNGP10.phx.gbl...
I have seen the "using" statement used in strange ways in some C# code.
Example:

using (StreamReader sr = new StreamReader("TestFile.txt"))
{
// do some stuff...
}

What effect does the using statement give you in this example?


It explicity forces the stream reader object to be 'cleaned up' when leaving
the using block. So, you do not have to worry about closing the file, etc,
that is all handled for you.

Personally, I don't care for it. I'd rather just say

sr.Close();

and be done with it.

--
Adam Clauss

Nov 17 '05 #6

P: n/a
But what if an unhandled exception was raised during the

// do some stuff

bit of code. Does the using (StreamReader sr = new
StreamReader("TestFile.txt"))
code construct automatically call the Dispose method as part of the stack
walk. If so, then it is similar to a try..finally and therefore must add
some value.

"Adam Clauss" wrote:
"Trevor" <tr****@spam.com> wrote in message
news:OK**************@TK2MSFTNGP10.phx.gbl...
I have seen the "using" statement used in strange ways in some C# code.
Example:

using (StreamReader sr = new StreamReader("TestFile.txt"))
{
// do some stuff...
}

What effect does the using statement give you in this example?


It explicity forces the stream reader object to be 'cleaned up' when leaving
the using block. So, you do not have to worry about closing the file, etc,
that is all handled for you.

Personally, I don't care for it. I'd rather just say

sr.Close();

and be done with it.

--
Adam Clauss

Nov 17 '05 #7

P: n/a
"Mike Read" <m1******@discussions.microsoft.com> wrote in message
news:B1**********************************@microsof t.com...
But what if an unhandled exception was raised during the

// do some stuff

bit of code. Does the using (StreamReader sr = new
StreamReader("TestFile.txt"))
code construct automatically call the Dispose method as part of the stack
walk. If so, then it is similar to a try..finally and therefore must add
some value.

Well of course, I simplified a bit. Definately would need a try catch in
there. So the using statement would let you not actually put the
try-finally in there.
But... if an exception is occuring, you are probably going to want to handle
it anyway (to print an error message or log something or whatever), so you
are still going to have the try-catch... might as well just add the finally.
That way I can see that I am explicity handling everything.

--
Adam Clauss
Nov 17 '05 #8

P: n/a
"Mike Read" <m1******@discussions.microsoft.com> wrote in message
news:B1**********************************@microsof t.com...
But what if an unhandled exception was raised during the

// do some stuff

bit of code. Does the using (StreamReader sr = new
StreamReader("TestFile.txt"))
code construct automatically call the Dispose method as part of the stack
walk. If so, then it is similar to a try..finally and therefore must add
some value.

Well of course, I simplified a bit. Definately would need a try catch in
there. So the using statement would let you not actually put the
try-finally in there.
But... if an exception is occuring, you are probably going to want to handle
it anyway (to print an error message or log something or whatever), so you
are still going to have the try-catch... might as well just add the finally.
That way I can see that I am explicity handling everything.

--
Adam Clauss
Nov 17 '05 #9

P: n/a
In this statement it means that StreamReader object sr will be valid
only for the code within parentheses and after it will be available for
garbage collection .

it can even be used for aliasing a big namespace.class combination with
in a parentheses , for it's ease of use .

Mrinal

Trevor wrote:
I have seen the "using" statement used in strange ways in some C# code.
Example:

using (StreamReader sr = new StreamReader("TestFile.txt"))
{
// do some stuff...
}

What effect does the using statement give you in this example?

Nov 17 '05 #10

P: n/a
In this statement it means that StreamReader object sr will be valid
only for the code within parentheses and after it will be available for
garbage collection .

it can even be used for aliasing a big namespace.class combination with
in a parentheses , for it's ease of use .

Mrinal

Trevor wrote:
I have seen the "using" statement used in strange ways in some C# code.
Example:

using (StreamReader sr = new StreamReader("TestFile.txt"))
{
// do some stuff...
}

What effect does the using statement give you in this example?

Nov 17 '05 #11

P: n/a
So it has the same effect as doing something like this?

try
{
StreamReader sr = new StreamReader("TestFile.txt");
// do some stuff.
}
catch (Exception e)
{
}
finally
{
try
{
sr.Close();
}
catch (Exception ex)
{
}
}
"Adam Clauss" <ca*****@tamu.edu> wrote in message
news:11*************@corp.supernews.com...
"Trevor" <tr****@spam.com> wrote in message
news:OK**************@TK2MSFTNGP10.phx.gbl...
I have seen the "using" statement used in strange ways in some C# code.
Example:

using (StreamReader sr = new StreamReader("TestFile.txt"))
{
// do some stuff...
}

What effect does the using statement give you in this example?


It explicity forces the stream reader object to be 'cleaned up' when
leaving the using block. So, you do not have to worry about closing the
file, etc, that is all handled for you.

Personally, I don't care for it. I'd rather just say

sr.Close();

and be done with it.

--
Adam Clauss

Nov 17 '05 #12

P: n/a
So it has the same effect as doing something like this?

try
{
StreamReader sr = new StreamReader("TestFile.txt");
// do some stuff.
}
catch (Exception e)
{
}
finally
{
try
{
sr.Close();
}
catch (Exception ex)
{
}
}
"Adam Clauss" <ca*****@tamu.edu> wrote in message
news:11*************@corp.supernews.com...
"Trevor" <tr****@spam.com> wrote in message
news:OK**************@TK2MSFTNGP10.phx.gbl...
I have seen the "using" statement used in strange ways in some C# code.
Example:

using (StreamReader sr = new StreamReader("TestFile.txt"))
{
// do some stuff...
}

What effect does the using statement give you in this example?


It explicity forces the stream reader object to be 'cleaned up' when
leaving the using block. So, you do not have to worry about closing the
file, etc, that is all handled for you.

Personally, I don't care for it. I'd rather just say

sr.Close();

and be done with it.

--
Adam Clauss

Nov 17 '05 #13

P: n/a
"Trevor" <tr****@spam.com> wrote in message
news:OJ**************@TK2MSFTNGP12.phx.gbl...
So it has the same effect as doing something like this?


Yes

--
Adam Clauss
Nov 17 '05 #14

P: n/a
"Trevor" <tr****@spam.com> wrote in message
news:OJ**************@TK2MSFTNGP12.phx.gbl...
So it has the same effect as doing something like this?


Yes

--
Adam Clauss
Nov 17 '05 #15

P: n/a
Trevor,

Not completely. It looks more like:

try
{
StreamReader sr = new StreamReader("TestFile.txt");
// do some stuff.
}
finally
{
((IDisposable) sr).Dispose();
}

This assumes it is a reference type that implements IDisposable. If it
was a structure, it would look different (depending on whether it was C# 2.0
or before).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Trevor" <tr****@spam.com> wrote in message
news:OJ**************@TK2MSFTNGP12.phx.gbl...
So it has the same effect as doing something like this?

try
{
StreamReader sr = new StreamReader("TestFile.txt");
// do some stuff.
}
catch (Exception e)
{
}
finally
{
try
{
sr.Close();
}
catch (Exception ex)
{
}
}
"Adam Clauss" <ca*****@tamu.edu> wrote in message
news:11*************@corp.supernews.com...
"Trevor" <tr****@spam.com> wrote in message
news:OK**************@TK2MSFTNGP10.phx.gbl...
I have seen the "using" statement used in strange ways in some C# code.
Example:

using (StreamReader sr = new StreamReader("TestFile.txt"))
{
// do some stuff...
}

What effect does the using statement give you in this example?


It explicity forces the stream reader object to be 'cleaned up' when
leaving the using block. So, you do not have to worry about closing the
file, etc, that is all handled for you.

Personally, I don't care for it. I'd rather just say

sr.Close();

and be done with it.

--
Adam Clauss


Nov 17 '05 #16

P: n/a
Trevor,

Not completely. It looks more like:

try
{
StreamReader sr = new StreamReader("TestFile.txt");
// do some stuff.
}
finally
{
((IDisposable) sr).Dispose();
}

This assumes it is a reference type that implements IDisposable. If it
was a structure, it would look different (depending on whether it was C# 2.0
or before).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Trevor" <tr****@spam.com> wrote in message
news:OJ**************@TK2MSFTNGP12.phx.gbl...
So it has the same effect as doing something like this?

try
{
StreamReader sr = new StreamReader("TestFile.txt");
// do some stuff.
}
catch (Exception e)
{
}
finally
{
try
{
sr.Close();
}
catch (Exception ex)
{
}
}
"Adam Clauss" <ca*****@tamu.edu> wrote in message
news:11*************@corp.supernews.com...
"Trevor" <tr****@spam.com> wrote in message
news:OK**************@TK2MSFTNGP10.phx.gbl...
I have seen the "using" statement used in strange ways in some C# code.
Example:

using (StreamReader sr = new StreamReader("TestFile.txt"))
{
// do some stuff...
}

What effect does the using statement give you in this example?


It explicity forces the stream reader object to be 'cleaned up' when
leaving the using block. So, you do not have to worry about closing the
file, etc, that is all handled for you.

Personally, I don't care for it. I'd rather just say

sr.Close();

and be done with it.

--
Adam Clauss


Nov 17 '05 #17

P: n/a
Adam Clauss <ca*****@tamu.edu> wrote:
Well of course, I simplified a bit. Definately would need a try catch in
there. So the using statement would let you not actually put the
try-finally in there.
But... if an exception is occuring, you are probably going to want to handle
it anyway (to print an error message or log something or whatever), so you
are still going to have the try-catch... might as well just add the finally.
That way I can see that I am explicity handling everything.


You shouldn't usually have a try/catch as well - you should normally
have far more try/finally blocks (usually in the form of a using
statement) than try/catch or try/catch/finally blocks - if you're
catching exceptions in most methods, you should consider a much less
fine-grained exception policy - after all, usually if an exception is
thrown, there's a fairly large block of work which can't proceed.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #18

P: n/a
Adam Clauss <ca*****@tamu.edu> wrote:
Well of course, I simplified a bit. Definately would need a try catch in
there. So the using statement would let you not actually put the
try-finally in there.
But... if an exception is occuring, you are probably going to want to handle
it anyway (to print an error message or log something or whatever), so you
are still going to have the try-catch... might as well just add the finally.
That way I can see that I am explicity handling everything.


You shouldn't usually have a try/catch as well - you should normally
have far more try/finally blocks (usually in the form of a using
statement) than try/catch or try/catch/finally blocks - if you're
catching exceptions in most methods, you should consider a much less
fine-grained exception policy - after all, usually if an exception is
thrown, there's a fairly large block of work which can't proceed.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #19

This discussion thread is closed

Replies have been disabled for this discussion.