Title: Should classes that are passed references to IDisposable objects be IDisposable as well and set them to null when disposed?
Tags: c#;reference;null;idisposable
Question: I think the question says it all.
Thanks.
Comment: That's a pretty odd meaning of "holding a reference" then. So your class is passed a reference (e.g. in the constructor) but doesn't take any responsibility for clean-up?

Comment: @Trap: better if you give a simplified example here.

Comment: Valuable edit. Makes quite a difference.

Comment: @Jon: That's it. Sorry if my English sound odd at times, I'm still learning :)

Here is the accepted answer: They should implement IDisposable if the object owns that resource. There's generally no need to set things to null, although it does no harm.
If you don't own the resource, then clearly you shouldn't dispose of it, and you don't need to implement IDisposable. In my experience it's relatively rare that I have a member variable for a disposable resource which I don't own in my classes though...
Comment for this answer: @Henk: That's true. It's very context-dependent, obviously :)
Comment for this answer: Jon, in the WinForms world components often 'link' to each other w/o owning something (DataSource). Whether that is such a good pattern is something else.

Here is another answer: "Holding a reference" is not the real criterium. When an object logically 'owns' an IDiposable object then Yes, it should implement IDisposable to call Dispose for all owned Disposable objects.
But it should not implement a destructor (Finalizer) .

Here is another answer: Yes they should if the reference is owned by the class, FxCop / MS code analysis even have a check for it.

Here is another answer: Setting to null isn't necessary, but you should call .Dispose() on all of those objects from the parent class's .Dispose().
EDIT: IF AND ONLY IF your class was responsible for creating those objects.
Comment for this answer: Only if that parent class is also responsible for creating the object. Cleaning up resources owned by other classes seems like a risky design to me.

Here is another answer: This a good practice yes. When the dispose of such a class is called, it's a good idea to call Dispose on all referenced objects as well.
Comment for this answer: It's not the same setting IDisposable references to null and calling Dispose on them, you would never want to do that.

Here is another answer: If your class creates the disposable object then your class is responsible for cleaning them up. Since your class won't know when to cleanup unless something tells it to (i don't count destructors) it is the easiest way to make your class implement IDisposable.
If your class uses, but does not create, disposable objects then you should leave cleaning up to the creator of those objects.
Comment for this answer: Ownership is not strictly determined as the creator. `StreamReader` takes a `Stream` (created elsewhere) in its constructor, but it logically owns the stream and `StreamReader.Dispose()` calls `Stream.Dispose()`.
Comment for this answer: The Creator is often, but not always the Owner.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 6, 'CommentCount': 4, 'ContentLicense': 'CC BY-SA 2.5', 'CreationDate': '2010:00:03/26/10', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<c#><reference><null><idisposable>', 'Title': 'Should classes that are passed references to IDisposable objects be IDisposable as well and set them to null when disposed?', 'ViewCount': 215, 'id': 2523685, 'user_AboutMe': "I'm a software developer working for a videogame company.", 'user_AccountId': 5006.0, 'user_DisplayName': 'Trap', 'user_Reputation': 11791.0, 'user_Views': 1131.0, 'user_id': 7839.0}}