C# code to handle different classes with same method names

前端 未结 9 1530
夕颜
夕颜 2020-12-10 01:56

Let\'s say you have two different C# classes A and B that while not deriving from the same base class do share some of the same names for methods.

9条回答
  •  醉梦人生
    2020-12-10 02:48

    What you want is called Duck Typing.

    From Wikipedia:

    Duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface.

    C# 4.0 allows this, as other have said, using the dynamic keyword

提交回复
热议问题