Declare IDisposable for the class or interface?

后端 未结 7 2221
心在旅途
心在旅途 2020-12-29 01:00

Starting from the follwing situation:

public interface ISample
{
}

public class SampleA : ISample
{
   // has some (unmanaged) resources that needs to be di         


        
7条回答
  •  旧时难觅i
    2020-12-29 01:52

    Following the Inteface Segregation Principle of SOLID if you add the IDisposable to the interface you are giving methods to clients that are not interested in so you should add it to A.

    Apart from that, an interface is never disposable because disposability is something related with the concrete implementation of the interface, never with the interface itself.

    Any interface can be potentially implemented with or without elements that need to be disposed.

提交回复
热议问题