Is method hiding ever a good idea

前端 未结 8 1811
死守一世寂寞
死守一世寂寞 2020-12-01 09:29

In C# the new modifier can be used to hide a base class method without overriding the base class method.

I\'ve never encountered a situation where hidin

相关标签:
8条回答
  • 2020-12-01 09:47

    There are rare, but very good, reasons to use method hiding. Eric Lippert posted a great example on his blog:

    interface IEnumerable<T> : IEnumerable { 
      new IEnumerator<T> GetEnumerator(); 
    }
    

    However, I think hiding should be the exception, and only used sparingly.

    0 讨论(0)
  • 2020-12-01 09:55

    The most common example I can think of here is things like DbCommand vs SqlCommand; the concrete types (SqlCommand etc) generally do a lot of method hiding to make the return types of properties / methods display the correct implementation type. This is because the related objects themselves have additional (implementation-specific) features, and the caller doesn't want to have to cast every time they call do anything.

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