开发者

when to use abstract class and when to use interface in c# [duplicate]

开发者 https://www.devze.com 2023-02-10 00:09 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicates: interface and abstract class.
This question already has answers here: Closed 12 years ago.

Possible Duplicates:

interface and abstract class.

Abstract classes vs Interfaces

Can anyone tell me when one should use an abstract class and when one should use an inte开发者_Python百科rface with sample scenarios?


If you're defining behavior (like IEnumerable, since a List acts like an IEnumerable), then use an interface, but if you're defining a kind of object (an IS-A) relationship, use an abstract class (like Stream -- since a MemoryStream IS-A Stream).


You should use an abstract class if you need to supply a base implementation.

Otherwise, use an interface.


When you want to provide an implementation of a method/set of methods but don't want people to directly instantiate your parent class, use an Abstract Class.

If you want to define what members a class must interface, but not provide any implementation, use an Interface.

0

精彩评论

暂无评论...
验证码 换一张
取 消