If a class inherits from an interface that itself implements IDisposable should that class also implement IDisposable or not?
e.g.
internal IMyInterface : IDisposable
{
Method1();
}
internal ClassA : IMyInterface, IDisposable
{
public Method1
{
...
}
public Dispose()
{
...
}
}
or
internal ClassA : IMyInterface
{
public Method1
{
...
}
public Dispose()
{
...
}
}
Any clarification on this matter would be most appreciated. Thanks
Interfaces are contracts and have no implementation of anything. Therefore a class implementing an interface MUST implement everything what the directly or indirectly inherited interfaces dictate. Your code won’t even compile if you don’t.
YES you must implement
IDisposableunless you derive your class from a class that already implements it. (Note: You do not need to specify theIDisposableinterface in the inheritance list of your class, but you must implement its members.)UPDATE
Note: Visual Studio makes it easy for you to implement an interface. A smart tag will automatically appear below
IMyInterfaceas long as you have not implemented it completely. You can choose between explicit and implicit implementation of the interface in the smart tag menu. Usually you would choose implicit. Then VS will automatically create the (empty) methods, properties and other members required by the interface, includingDispose.