473,508 Members | 2,337 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3022
[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
433
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
3935
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
2997
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
2224
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
2104
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
2369
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
1493
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
1929
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
1513
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
3252
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
7225
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7383
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7046
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5627
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5053
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1557
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
418
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.