隐藏方法是否是一个好主意 [英] Is method hiding ever a good idea

查看:55
本文介绍了隐藏方法是否是一个好主意的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中, new 修饰符可用于隐藏基类方法,而不会覆盖基类方法。



<我从来没有遇到过隐藏方法是最好的选择的情况。有什么情况是最好的方法隐藏方法?

解决方案

使用方法隐藏方法的原因很少,但是非常好。埃里克·利珀特(Eric Lippert)发布了一个很好的例子他的博客:

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

但是,我认为隐藏应该是例外,只能谨慎使用。 / p>

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 hiding a method was the best choice available. Are there any situations where method hiding is the best choice?

解决方案

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.

这篇关于隐藏方法是否是一个好主意的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆