C# 和方法隐藏
根据 MSDN,“new”关键字在用于方法隐藏时只会抑制警告。
http://msdn.microsoft.com/en-us/library/435f1dw2。 aspx
我真的需要这个“new”关键字来执行方法隐藏吗? 如果我有一个具有相同方法名称但没有明确声明它是重写的子类,那么本质上不是同一件事吗?我只是收到警告? 请告诉我我的理解是否正确。 谢谢
Per MSDN, the "new" keyword when used for method hiding only suppresses a warning.
http://msdn.microsoft.com/en-us/library/435f1dw2.aspx
Do I really need this "new" keyword to perform method hiding? If I have a child class that has the same method name but doesn't explicitly state that it is overriding, isn't that the same thing essentially, I just get a warning? Please tell me if my understanding is correct. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
无论是否指定“new”,方法都会隐藏,但这与覆盖不同。 下面是它们不同的示例:
输出为:
尽管基本
HideMe
方法是虚拟的,但它不会在Derived
中被覆盖,它只是被隐藏 - 所以方法仍然绑定到Base
中的虚拟方法,这就是执行的方法。成员隐藏通常是一个坏主意,这会使代码更难理解。 事实上,它的可用在版本控制方面是有益的,但是,这意味着向基类添加方法不会让派生类无意中覆盖它,并且它们可以像以前一样继续工作。 这就是您收到警告的原因。
You get method hiding whether or not you specify "new", but it's not the same as overriding. Here's an example where they're different:
The output is:
Even though the base
HideMe
method is virtual, it isn't overridden inDerived
, it's just hidden - so the method is still bound to the virtual method inBase
, and that's what gets executed.Member hiding is generally a bad idea, making the code harder to understand. The fact that it's available is beneficial in terms of versioning, however - it means adding a method to a base class doesn't potentially let derived classes override it unintentionally, and they can keep working as before. That's why you get the warning.
隐藏(隐藏)方法时应始终使用 new 关键字。 尽管从技术上讲,它不会对功能产生影响,但正如您正确指出的那样,强烈建议这样做。
关键字的出现不仅向代码读者清楚地表明您在基类中显式隐藏了同名的方法,而且它的用法是官方 (MSDN) C# 指南的一部分,很可能是因为它将来可能需要使用。
当您省略关键字时,当前编译器仅向您发出警告(而不是错误),这纯粹是出于向后兼容性的原因。 正如 MSDN 所建议的那样,C# 1.0 不支持使用 new 关键字来隐藏类成员,并且会自动执行方法(通常是成员)隐藏。 我怀疑 MS 会尝试在这方面保持向后兼容性,但在未来的 C# 版本中肯定不能保证这一点。
The
new
keyword should always be used when hiding (shadowing) methods. Although technically it does not make a difference to functionality, as you correctly point out, it is strongly recommended.The prescence of the keyword not only indicates clearly to the reader of the code that you are explicitly hiding a method of the same name in the base class, but also its usage is part of the official (MSDN) C# guidelines, quite possibly because its usage may be required in the future.
The reason that the current compiler only gives you a warning (rather than an error) when you omit the keyword is purely for reasons of backward compatibility. C# 1.0 does not support the
new
keyword for hiding members of classes, as MSDN suggests, and method (generally member) hiding was performed then automatically. I suspect MS will try to maintain backward compatibility in this respect, but it's certainly not guaranteed in future versions of C#.在隐藏方法时使用 new 关键字是为了“使程序员的意图清晰”。 这可以避免程序员意外隐藏。
如果 new 关键字不存在,则编译器会发出警告并将其视为存在。
您可以阅读有关它的更多信息 使用 Override 和 New 关键字进行版本控制(C# 编程指南)
Use of new keyword while hiding a method is to "make the intent of the programmer clear". This avoids accidental hidings by the programmers.
If the new keyword is not present then the compiler issues a warning and treats it as if it was present.
You can read more about it Versioning with the Override and New Keywords (C# Programming Guide)
方法隐藏与重写不同。 不要在您应该选择覆盖的情况下使用它,因为它们的工作方式不同。
我前段时间写过这个,你可能想阅读 方法隐藏或覆盖 - 或 new 和 virtual 之间的区别了解更多详细信息。
不应该鼓励方法隐藏,因为它会改变类的语义。 功能会根据您是否有对基类或实际类的引用而变化。 这是编译器发出警告的主要原因。 如果调用者不知道正在使用方法隐藏,它可能会做出意想不到的事情。
附加
基于问题的更新。 方法隐藏不需要关键字
new
,尽管您应该明确它并使用关键字(如果您正在这样做)。 它删除了编译器警告并提醒其他开发人员(或 6 个月后的您自己)您的意图是什么。我仍然不建议隐藏方法,不过,
更新:2011 年 14 月 5 日
我移动了我的博客,因此我正在更新所有旧链接
Method hiding is not the same as overriding. Do not use it where overriding should be your choice as they work differently.
I wrote about this some time ago, you might want to read Method hiding or overriding - or the difference between new and virtual for more details.
Method hiding should be discouraged because it changes the semantics of a class. The functionality changes depending on if you have a reference to the base, or the actual class. That's the main reason there is a compiler warning about it. It can do unexpected things if the caller is not aware that method hiding is in use.
ADDITIONAL
Based on updates to the question. Method Hiding does not require the keyword
new
, although you should make it explicit and use the keyword if that is what you are doing. It removes the compiler warning and alerts other developers (or yourself 6 months down the line) what your intention was.I still wouldn't recommend method hiding, tho'
UPDATE: 14/5/11
I moved my blog, so I'm updating any old links
使用“new”和“不使用new”之间的唯一区别是,当您不使用“new”关键字时,您将收到编译器警告,以提醒开发人员任何无意的方法隐藏正在发生的情况。 在以下 URL,您还可以找到一个精彩的视频,解释 方法隐藏以及使用“new”和“不使用new”的区别。
The only difference between using "new" and "not using new" is that, you will get a compiler warning when you don't use the "new" keyword, to alert the developer of any inadvertent method hiding that is happening. At the following URL you can also find an excellent video explaining method hiding and the difference between using "new" and "not using new".
它是 C# 的一部分,就像 ref 和 out 一样。 这个想法是警告您,您的 API 正在重写一个方法,该方法可能会影响依赖于您的类的子类/超类的代码。
放置 new 关键字会迫使您思考这是否真的是您想要做的事情,就像需要 ref 或 out 一样,以便您必须考虑代码提供的合同。
It's part of C#, just like ref and out. The idea is to warn you that your API is overriding a method which could affect code that relies on sub/superclasses of your class.
Placing the new keyword forces you to think about whether that is really what you intended to do or not, just like requiring ref or out so that you have to think about the contract your code is providing.
据我所知,唯一的区别是您建议的:
new
隐藏警告。 我尝试了以下代码:两个类的测试方法在 IL 中看起来相同:
编译器在
ClassA
中发出了针对Test
的警告,但在ClassB< 中则没有发出警告。 /代码>。 但正如其他答案所说; 不要混淆方法隐藏和方法重写的概念; 不是同一件事。
As far as I know, the only difference is the one you suggest:
new
hides the warning. I tried out the following code:The test method for both classes look identical in IL:
The compiler issued a warning for
Test
inClassA
, but not inClassB
. But as other answers has stated; don't confuse the concepts of method hiding and method overriding; not the same thing.new 的使用是当你想要一个同名的函数,但又不想要的时候
想要覆盖。 它打破了虚拟调度链。
当有人将函数添加到具有以下内容的基类时,可能会发生这种情况
与派生类函数的名称相同。
The use of new is when you want a function of the same name, but you don't
want to override. It breaks the virtual dispatch chain.
This might happen when somebody adds a function to a base class that has the
same name as your derived class function.
多谢你们。 我知道隐藏和覆盖之间的区别,我只是好奇使用“new”和不使用“new”进行隐藏之间是否存在功能差异。 根据您所有的答案,似乎并非如此,尽管有人提出了一个很好的观点,因为它可以使代码更清晰,并且将来可能是必要的。 再次感谢各位!
Thanks guys. I am aware of the differences between hiding and overriding, I was just curious if there was a functionality difference between using "new" and not using "new" for hiding. According to all of your answers, it seems not although as someone had raised a good point in that it makes for cleaner code and it may be necessary in the future. THanks again Folks!