I have a class which has a property of the type SqlConnection
. SqlConnection
implements IDisposable
. I have following questions:
Should my class also implement
IDisposable
just because it has property of typeIDisposable
?If yes, do I need to
Dispose
the property explicitly when I am disposing instance of my class? E.g.public class Helper : IDisposable { // Assume that it's ANY OTHER IDisposable type. SqlConnection is just an example. public SqlConnection SqlConnection { get; set; } public void Dispose() { if (SqlConnection!= null) { SqlConnection.Dispose(); } } }
Note :
I know that there is a pattern to be followed while implementing IDisposable
but my question is very specific to the case mentioned above.