问题
To me, it sounds more like an exception to hide a member in the base class in order to hack something instead of refactoring a bad design...
Maybe I am wrong. But...
Is it a code smell to hide a member in the base class with 'new' keyword?
回答1:
It depends on the intent. If the purpose is to clarify the concrete return type, for example, then it might not be unreasonable. The classic example here is DbConnection
. It is useful that SqlConnection
re-declares CreateCommand
etc, by returning SqlCommand
rather than DbCommand
, since SqlCommand
has many platform-specific features that would otherwise be a pain. A key point, here, though is that the DbConnection
method still works, and the purpose and meaning of the two methods is the same.
If you are doing this and the methods do something different, or return something different, then there are occasions it can cause confusion. And times it can still make sense ;p If they do the same thing, and only vary by formally stating something about the implementation that is useful to the caller, then fine.
回答2:
May be this can help. http://blogs.msdn.com/b/ericlippert/archive/2008/05/21/method-hiding-apologia.aspx
来源:https://stackoverflow.com/questions/9462393/is-hiding-a-member-in-the-base-class-a-bad-smell-in-the-code