473,662 Members | 2,382 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,blo gs,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 3038
[I don't think it was worth cross-posting this *quite* so widely, btw.
There's also no such group as microsoft.publi c.dotnet.csharp .general]

Mehul Patel <mp****@focus-technologies.co m> 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,blo gs,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.co m>
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
[I don't think it was worth cross-posting this *quite* so widely, btw.
There's also no such group as microsoft.publi c.dotnet.csharp .general]

Mehul Patel <mp****@focus-technologies.co m> 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" <davidbaxterbro wne no potted me**@hotmail.co m>> 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.co m>
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 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
9
3958
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 will not set the value of the control and will leave the checked status of the checkbox to false when a user selects a new date. this works fine when using the control on a win2k machine but if we use it on a win XP box and call
7
3010
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 "Close()" methods reared their ugly (at least it would seem...)heads. I was happily delving away in the .NET framework, investigating the stream classes with the msdn and Lutz Roeder's .NET reflector, when I stumbled upon the following:
4
2234
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
2123
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()) doSomething(reader); } I have seen examples here and there with mixed assumptions.
1
2377
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 look for when evaluating someone's code is a try/catch block. While it isn't a perfect indicator, exception handling is one of the few things that quickly speak about the quality of code. Within seconds you might discover that the code author...
2
1503
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)) { connection.Open(); .... connection.Close();
10
1951
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, CN) Dim DR As OleDbDataReader = CMD.ExecuteReader()
9
1523
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
3267
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 = new SqlConnection(ConfigurationManager.ConnectionStrings.ToStri ng())) { using (SqlCommand _comm = new SqlCommand("GetRevenueByOwner", _conn))
0
8432
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8856
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8762
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7365
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6185
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5653
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4179
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2762
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 we have to send another system
2
1747
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.