Does Array.Add method violate LSP?

Arraya class implements an interface IListthat a member has Add. Array.Addinvocation throws NotSupportedException. Is this a violation of the Liskov Substitution Principle or the Segregation Separation Principle, or both of them?

+4
source share
1 answer

This, of course, is a violation of the principle of interface segregation. ISP states that components should depend only on the interfaces that they really require. Arrayimplements IList, but clearly does not need or wants, all methods defined IListbecause he chooses an exception to explain that he does not support Add. In addition, everything that he needs Arraynow has IList, despite the fact that he does not need all of his methods (since everything that the array wants to receive is clearly not worried about Add, since it still does not work). Not supporting the operation that your interface offers you to implement is clearly an ISP violation; forcing consumer code to depend on an interface, even though it doesn't need all of this, is also an ISP violation.

. IList, LSP , IList. Array , Add.

LSP, , LSP. , , . , , Add , , .

, NotSupportedException , LSP. , SOLID , , , , : " ", ", " - , , , . , , , Array Add , , LSP.

+6

Source: https://habr.com/ru/post/1686614/


All Articles