473,385 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Using Keyword Dispose and GC

Our .Net team have been pondering about using keyword.

We are using streams FileStream and BufferedStream. We use using
keyword at FileStream, and not BufferedStream which wraps FileStream.
So bunch of people said what if exception occurs buffered stream is
not closed.

My Argument is over advantages of using 'Using' keyword.

First Advantage being -- Using used for Dispose (Deterministic
Finalization) to free resources earlier, than later. Dispose and Close
do the same thing. This statement is valid, and is verified by
numerous sources (msdn,books,blogs,newsgroups).

Second Advantage being all objects in using bracket will have priority
in Gargbage collection ! - Is this statement valid?

If file stream is grabage collected, there is no need to use Using
keyword on BufferedStream -- Is this statement valid?.

Pl shed some light on this.
Thanks Very much.
Mehul.
Nov 22 '05 #1
3 3011
[I don't think it was worth cross-posting this *quite* so widely, btw.
There's also no such group as microsoft.public.dotnet.csharp.general]

Mehul Patel <mp****@focus-technologies.com> wrote:
Our .Net team have been pondering about using keyword.

We are using streams FileStream and BufferedStream. We use using
keyword at FileStream, and not BufferedStream which wraps FileStream.
So bunch of people said what if exception occurs buffered stream is
not closed.
I would expect that the buffered stream wouldn't actually have any
unmanaged resources itself. On the other hand, it wouldn't hurt to be
sure, and it would add to consistency.
My Argument is over advantages of using 'Using' keyword.

First Advantage being -- Using used for Dispose (Deterministic
Finalization) to free resources earlier, than later. Dispose and Close
do the same thing. This statement is valid, and is verified by
numerous sources (msdn,books,blogs,newsgroups).
Yes.
Second Advantage being all objects in using bracket will have priority
in Gargbage collection ! - Is this statement valid?
No, I don't believe that's valid at all.
If file stream is grabage collected, there is no need to use Using
keyword on BufferedStream -- Is this statement valid?.


It's *probably* okay, but I would personally use the using statement
with the BufferedStream as well. In fact, I'd probably just have one:

using (BufferedStream stream = new BufferedStream (new FileStream(...))
{
....
}

Out of interest, are you sure the BufferedStream is helping you? I
*thought* FileStreams were already buffered, although I could be wrong.

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

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
[I don't think it was worth cross-posting this *quite* so widely, btw.
There's also no such group as microsoft.public.dotnet.csharp.general]

Mehul Patel <mp****@focus-technologies.com> wrote:
Our .Net team have been pondering about using keyword.

We are using streams FileStream and BufferedStream. We use using
keyword at FileStream, and not BufferedStream which wraps FileStream.
So bunch of people said what if exception occurs buffered stream is
not closed.
I would expect that the buffered stream wouldn't actually have any
unmanaged resources itself. On the other hand, it wouldn't hurt to be
sure, and it would add to consistency.


It is an error to close the underlying stream before the BufferedStream,
since any buffered data will not have been written to the file. You should
use the using block on the BufferedStream. BufferedStream.Close calls
BufferedSteram.Flush then Stream.Close.

.. . .
using (BufferedStream stream = new BufferedStream (new FileStream(...))
{
...
}
Exactly.

Out of interest, are you sure the BufferedStream is helping you? I
*thought* FileStreams were already buffered, although I could be wrong.


FileStream is not buffered in managed memory, it just calls WriteFile. But
NTFS does various things to make writing files fast. That being said, the
priority is to get written data safely on disk, so if you have a lot of very
small writes I suppose a BufferedStream could improve your performance.

David
Nov 22 '05 #3
<"David Browne" <davidbaxterbrowne no potted me**@hotmail.com>> wrote:
I would expect that the buffered stream wouldn't actually have any
unmanaged resources itself. On the other hand, it wouldn't hurt to be
sure, and it would add to consistency.


It is an error to close the underlying stream before the BufferedStream,
since any buffered data will not have been written to the file. You should
use the using block on the BufferedStream. BufferedStream.Close calls
BufferedSteram.Flush then Stream.Close.


Ah, very true. I'd forgotten about the flush issue...

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mehul Patel | last post by:
Our .Net team have been pondering about using keyword. We are using streams FileStream and BufferedStream. We use using keyword at FileStream, and not BufferedStream which wraps FileStream. So...
9
by: Guy | last post by:
I have extended the datetimepicker control to incorporate a ReadOnly property. I have used the new keyword to implement my own version of the value property, so that if readonly == true then it...
7
by: Willem van Rumpt | last post by:
Hi all, coming from an unmanaged programming background, I took my time to sort out the IDisposable and finalizer patterns. Just when I thought I had it all conceptually neatly arranged, the...
4
by: Peter Kirk | last post by:
Hi I have some code in C# for accessing a database. For example: using (SqlConnection conn = GetConnection() ) { using (SqlCommand command = new SqlCommand( ... { ... }
10
by: INeedADip | last post by:
Can anyone here confirm for sure that reader.Close() and Dispose() will get called using the following routine: using(SqlDataReader reader = GetOpenReaderFunction()) { while(reader.Read())...
1
by: Anonieko | last post by:
Understanding and Using Exceptions (this is a really long post...only read it if you (a) don't know what try/catch is OR (b) actually write catch(Exception ex) or catch{ }) The first thing I...
2
by: R.A.M. | last post by:
Hello, I have started larning C# and I have a question concerning "using (...)" keyword. For example: using (SqlConnection connection = new SqlConnection(ConnectionString)) {...
10
by: mg | last post by:
I'm migrating from VB6 and have a question about using 'Using' and the best way to use it. Here is a example of a small bit of code: dbConx("open") Using CN Dim CMD As New OleDbCommand(sSQL,...
9
by: Menny | last post by:
Hi, I'm looking for a way to determine if the 'Dispose()' function at the end of a 'using' block, was called due to an exception. Can anyone help?
1
by: Mike P | last post by:
I've recently started using the using keyword for my connections, but I have just seen some code where you have a using within a using, which is opening a command : using (SqlConnection _conn =...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.