Re: Use 'new' keyword...

Fabian Schmied <[email protected]> Thu, 2 Nov 2006 20:32:05 +0100
Newsgroups gmane.comp.windows.devel.dotnet.cx
Message-ID <[email protected]>
> Ahhh, thank you both.  Somehow I missed the MSDN page on 'new' as a modifier
> (which I have now reviewed).

You probably already know this, but just in case you don't: you should
also think about if "new" is really the right choice. In practice,
"new" means your objects will have two methods of the same name (and
parameter lists), which can be confusing.

Consider this:
B b1 = new B();
A b2 = new B(); // this works because B is a subclass of A

b1.Copy(); // calls B's Copy -> returns a new B object, a copy of b1
b2.Copy(); // calls A's Copy -> returns a new A object, a copy of the
"A part" of b2

In most cases, you actually want to use override instead of new, so
that B's Copy method replaces the one defined by A, and b2.Copy will
correctly create a full-featured B object as well.

In those cases, where override is not what you want, you should think
about using different names for the different methods.

(Side note: The .NET framework uses the term "cloning" for making
copies of objects. There's also an interface ICloneable that
can/should be implemented by objects that support being cloned.)

Regards,
Fabian

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com