I recently read this in "CLR via C#" by Jeffery Richter;
Important If a class defines a field in which the field’s type implements the dispose pattern, the class itself should also implement the dispose pattern. The Dispose method should dispose of the object referred to by the field. This allows someone using the class to call Dispose on it, which in turn releases the resources used by the object itself.
Would this be true in the following example?
public class SomeClass
{
private readonly StreamReader _reader; //a disposable class
public SomeClass(StreamReader reader)
{
_reader = reader;
}
}
Although StreamReader is a disposable class its intance has been passed in via the constructor, it will therefore probably be referenced somewhere else and so implementing IDisposable on SomeClass so that _reader can be disposed seems like a bad idea. Is the point that Jeffery Richter is trying to make only applicable to classes where the instances of disposable classes are instantiated within that class?