Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Valid applications of member hiding with the new keyword in c#

Tags:

I've been using c# since version 1, and have never seen a worthwhile use of member hiding. Do you know of any?

like image 663
James L Avatar asked May 06 '10 20:05

James L


People also ask

What is new keyword in case of method hiding?

The "new" keyword is used to hide a method, property, indexer, or event of the base class into the derived class. If a method is not overriding the derived method then it is hiding it. A hiding method must be declared using the new keyword. Shadowing is another commonly used term for hiding.

What is the use of new keyword in inheritance in C#?

When used as a declaration modifier, the new keyword explicitly hides a member that is inherited from a base class. When you hide an inherited member, the derived version of the member replaces the base class version.

Where have you used new keyword Apart from creating a new instance or what is method hiding shadowing?

It is also known as Method Shadowing. In method hiding, you can hide the implementation of the methods of a base class from the derived class using the new keyword. Or in other words, in method hiding, you can redefine the method of the base class in the derived class by using the new keyword.

What is the difference between new and override keyword in C#?

In C#, a method in a derived class can have the same name as a method in the base class. You can specify how the methods interact by using the new and override keywords. The override modifier extends the base class virtual method, and the new modifier hides an accessible base class method.


1 Answers

Scenario #1:

Imagine you are designing the runtime library for .NET 2.0. You now have generics at your disposal. You have an interface:

interface IEnumerable  {     IEnumerator GetEnumerator(); } 

You wish to make a new interface

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

You now have three choices.

1) Make the generic version unrelated to the non-generic version.

2) Make the generic version extend the non-generic version. You now have two methods that differ only in return type. Change the name of GetEnumerator in the new type to GetEnumerator2(). Because that's hot. Everyone loves a good "2" method.

3) Make the generic version extend the non-generic version. Make the new and improved method hide the existing method so that its there if you need it, but hidden by default.

These are all bad choices. Which would you choose? We chose (3). Good thing that it was an option; without hiding, that option would not have been available.

Now, you might argue that this particular example of hiding was not 'worthwhile'; if that's so, what would you have done instead?

Method hiding makes it possible to expose improved interfaces without causing breakages when there are improvements to the type system.

Scenario #2:

You work at FrobCo. You produce a class Frobber that extends Blobber, which is supplied to you by the good people at BlobCo.

BlobCo has neglected to put a Frobozzle() method on Blobber, but your customers love to frobozzle frobbers, so you add a method Frobozzle() to derived class Frobber.

BlobCo realizes that their customers want to Frobozzle blobbers, so they add a non-virtual method Frobozzle() to Blobber, the base class.

Now what do you do, FrobCo employee?

1) Remove the Frobozzle method on Frobber, thereby breaking your customers who relied on your implementation. Remember, BlobCo doesn't know how to Frobozzle a Frobber; they only wrote code that knows how to Frobozzle a Blobber.

2) Whine to BlobCo that they should have made their method virtual. Hope they do something about it someday.

3) Hide their method in your derived class.

Method hiding helps mitigate the brittle base class problem.

Further Reading:

http://blogs.msdn.com/ericlippert/archive/2008/05/21/method-hiding-apologia.aspx

like image 137
Eric Lippert Avatar answered Nov 15 '22 12:11

Eric Lippert