Extension interface patterns

后端 未结 11 2025
失恋的感觉
失恋的感觉 2020-12-14 16:56

The new extensions in .Net 3.5 allow functionality to be split out from interfaces.

For instance in .Net 2.0

public interface IHaveChildren {
    str         


        
11条回答
  •  无人及你
    2020-12-14 17:03

    There is nothing wrong with extending interfaces, in fact that is how LINQ works to add the extension methods to the collection classes.

    That being said, you really should only do this in the case where you need to provide the same functionality across all classes that implement that interface and that functionality is not (and probably should not be) part of the "official" implementation of any derived classes. Extending an interface is also good if it is just impractical to write an extension method for every possible derived type that requires the new functionality.

提交回复
热议问题