c# Extension methods - design patterns

后端 未结 10 3487
予麋鹿
予麋鹿 2021-02-20 16:33

I would like to know if C# extension method is based on any existing design pattern.

相关标签:
10条回答
  • 2021-02-20 17:05

    The best matching design pattern to extension method is Facade pattern. My Reason: We usually use extension methods not to introduce a new functionality beyond the target class responsibility, but simplifying using existing target class usage. Because simplifying the target class usage is the Facade pattern concern, extension methods can alternatively be implemented using the Facade pattern. There are some problems in extending and unit testing extension methods. So I think implementing Facade pattern is a better approach against using extension methods. However it is possible to implement some extension methods that wrap the facade interface in order to provide a coding facility for client codes.

    0 讨论(0)
  • 2021-02-20 17:09

    No. It's just a language feature.

    0 讨论(0)
  • 2021-02-20 17:09

    No, they are not, because they are only syntactic sugar.

    0 讨论(0)
  • 2021-02-20 17:12

    I wouldn't label Extension Methods as any of the common design patterns, but it can be used to implement patterns like, Decorator and Adapter etc..

    0 讨论(0)
提交回复
热议问题