Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
323 views
in Technique[技术] by (71.8m points)

c# - benefit of using new keyword in derived class member having same name with base class member

The C# language specification says that if I inherit a class, and the base class and derived class have the same named member with the same signature, then I have to use the new keyword to hide the base class member (There is another way by using virtual and override keyword in base and derived class member).

But in practice I found that the derived class auto hides the derived member if it has the same named member. So what is the main benefit and problem new keyword in same named derived class member?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

New is not required, as you note. It is optional, and if you do not use it, you get a warning. You are entirely correct to note that this is at first glance a strange design decision.

The purpose of this design decision is to help mitigate a class of problems known as the "Brittle Base Class" problems. Here's a version of that problem:

Foo Corporation creates a class Frobber and ships it in Foo.DLL version 1.0:

namespace FooCorp
{
  public class Frobber
  {
    public void Frobnicate() { ... }
    ...

Bar Corporation, who you work for, makes Blobbers. A Blobber can do everything that a Frobber can do, but in addition, it can Blobnicate too. So you decide to re-use the implementation of Frobnicate from FooCorp, and add some additional functionality:

namespace BarCorp
{
  public class Blobber : FooCorp.Frobber
  {
    public void Blobnicate() { ... }
    ...

Foo Corporation realizes that people like to Blobnicate, and they decide to ship Foo.DLL v2.0:

namespace FooCorp
{
  public class Frobber
  {
    public void Frobnicate() { ... }
    public void Blobnicate() { ... }
    ...

When you get a new version of Foo.DLL and recompile, you want to be told that you are now accidentally introducing a new method that shadows a base class method. That is possibly a dangerous thing to do; your class was written with the assumption that the base class was a Frobnicator, but apparently now it is a Blobnicator too! That fact could break your customers, who might accidentally call the base class version when they intended to call your derived class version.

We make "new" optional so that it is legal for you to shadow a base class method without changing your source code. If we made it illegal then FooCorp would have broken your build with their upgrade. But we make it a warning so that you know that you might be doing so accidentally. You can then examine the code carefully; if you decide that your implementation of Blobnicate is now redundant, you can remove it. If it is still good, you can mark it as "new" and eliminate the warning.

Make sense? This is one of the subtle features of C# that make it suitable for large scale multi-version component-oriented software.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...