Billkamm

When you have a method name that is the same in the base class and the child class which is the best option to use between making the base method virtual and overriding it, or using "new" to hide the base class's implementation.

I guess I'm looking for an explanation of when NOT to use virtual and to use "new" in the child class instead.


Re: Visual C# Language Use new or override?

ArranNyc

You can find a good explanation here :

http://blogs.msdn.com/csharpfaq/archive/2004/03/12/88453.aspx





Re: Visual C# Language Use new or override?

Peter Ritchie

When you use new you're only hiding as that type of object. For example, if you have a base class virtual Base.Method and you hide that in a defined class (say Derived.Method). If you access Method through a Base reference it will still call the Base Method. Whereas, if you use override you'll still call the derived method.

For example:

Code Snippet
Derived derived = new Derived();
derived.Method();
BaseClass baseObject = derived;
baseObject.Method();


if Derived.Method were declared with new BaseClass.Method would be called on the last line.