I've been using c# since version 1, and have never seen a worthwhile use of member hiding. Do you know of any?
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.
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.
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.
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.
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.
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.
http://blogs.msdn.com/ericlippert/archive/2008/05/21/method-hiding-apologia.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With