-2

Want to understand what part of StreamWriter source code is Unmanaged code. Have went through the code in http://referencesource.microsoft.com/ website.

But it seems to be complex code to understand,there is good comments in the source code. But still had tough time to understand, may be my knowledge is not upto that mark. However , if anybody has any blog or article that can answer this question. it will be great !!!

sameer
  • 1,635
  • 3
  • 23
  • 35

2 Answers2

3

StreamWriter is not an unmanged resource, its a .NET class, and it is 100% managed.

Another totally different thing is that StreamWriter might internally use unmanaged resources or own an IDisposable object that in turn might use an unmanaged resource, or simply extends a class that implements IDisposable.

The latter two are the reasons why StreamWriter implements IDisposable, but beware, implementing IDisposable does not necessarily mean that the class uses directly or indirectly unmanaged resources.

In the particular case of StreamWriter, it is obvious that it might indirectly consume unmanged resources; the underlying stream (the IDisposable instance field Stream stream) could be a FileStream which obviously consumes unmanaged resources (a file in your HD for instance). But its also very possible that the underlying stream doesn't use any unmanaged resources, but as Colin Mackay correctly points out in commentaries below, all streams must implement a consistent interface which the abstract class Stream provides.

Community
  • 1
  • 1
InBetween
  • 32,319
  • 3
  • 50
  • 90
  • thanks, want to understand what is that streamwriter use that enforce the implementation of IDisposable interface. – sameer Aug 22 '16 at 12:53
  • 1
    @sameer Two reasons. First and foremost, becuase it derives from `TextWriter` which implements `IDisposable`. Second because it internally uses a `Stream` field which is an `IDisposable` object (one of the reasons I gave where your should implement `Dispose()`). – InBetween Aug 22 '16 at 12:59
  • Whether the underlying stream is managed or not, it must (as must the streams themselves) still have a consistent interface for any callers otherwise you break the Liskov Substitution Principle. – Colin Mackay Aug 22 '16 at 13:01
  • 1
    @ColinMackay Exactly, `Stream` is not unmanaged nor does it use any unmanaged resources. Its simply an ´abstract´ type providing the consistent interface you refer to; wether the concrete underlying stream uses unmanaged resources or not is not relevant. – InBetween Aug 22 '16 at 13:05
0

StreamWriter is managed, but it's a convention that disposing a .NET Stream object will always dispose of any underlying streams. Streams are often constructed as containers or wrappers around other streams. For example, the StreamWriter might be a wrapper around a FileStream (e.g. when you create your StreamWriter using System.IO.File.CreateText). Since a FileStream uses managed resources, and you don't necessarily have a direct reference to it, it's important to be able to dispose it by calling Dispose() on the StreamWriter.

A FileStream contains managed resources in the form of OS-level file handles.

RogerN
  • 3,761
  • 11
  • 18