Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
494 views
in Technique[技术] by (71.8m points)

c# - Understanding Streams and their lifetime (Flush, Dispose, Close)

Note: I've read the following two questions already:

Can you explain the concept of streams?

C# using streams

I'm coding in C#

  1. In almost all code samples that use streams, .Dispose(), .Flush(), .Close() are almost always called.
    • In the concept of a stream, what does accomplish?
    • If I don't dispose a stream that I stored in a variable, is my application leaking somewhere?
    • Why do I need to call any of these functions? I've seen code samples that don't do this and still get the job done (without an apparent breakage)

I'm currently building a class in my application that contains a primary method (lets call it GetStream()) that returns a stream via myWebRequest.GetResponse().GetResponseStream()

The primary method GetStream() returns a Stream object that can be used for any operation that requires a stream (StreamReader, Bitmap() etc.).

Is there a way to have the stream Disposed of automatically after its last use (garbage collected?) without forcing anyone that calls .GetStream() to manually dispose of it?

As you can probably tell, my questions are vague and general. My understanding of streams is not solid, so any links to helpful articles that offer a more in-depth look at streams than a SO question can offer would be appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Disposing a stream closes it (and probably doesn't do much else.) Closing a stream flushes it, and releases any resources related to the stream, like a file handle. Flushing a stream takes any buffered data which hasn't been written yet, and writes it out right away; some streams use buffering internally to avoid making a ton of small updates to relatively expensive resources like a disk file or a network pipe.

You need to call either Close or Dispose on most streams, or your code is incorrect, because the underlying resource won't be freed for someone else to use until the garbage collector comes (who knows how long that'll take.) Dispose is preferred as a matter of course; it's expected that you'll dispose all disposable things in C#. You probably don't have to call Flush explicitly in most scenarios.

In C#, it's idiomatic to call Dispose by way of a using block, which is syntactic sugar for a try-finally block that disposes in the finally, e.g.:

using (FileStream stream = new FileStream(path))
{
    // ...
}

is functionally identical to

FileStream stream;

try
{
    stream = new FileStream(path);
    // ...
}
finally
{
    if (stream != null)
        stream.Dispose();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...