This is ported from my old weblog. Originally published April 07, 2005
There was a question in Microsoft Forums regarding the new modifier in C# and what’s the difference between using it and using the virtual and override modifiers. I wanted to share that here as well.
The new modifier is mainly used for hiding the non virtual methods. Unlike override modifier, it’s used to hide all class members not only methods (i.e. variables and properties). If you create a Base class with a protected, internal or public int x for example, and inherit that Base in another class called Child, placing another x in it and try to compile this, the code will compile successfully! However, if you look at the tasks window (assuming that you are using VC#) you’ll see a warning that you should use the new keyword with the second declaration. A simple code should tell you what I mean here:
Anyway, this is not the primary purpose for the modifier. The main purpose comes when you use it to hide a method instead of using override (by the way, to hide a method, you can’t use both override and new. This gives you an error not just a waning).
Using virtual and override modifiers will always be similar to using the new modifier except that when you use virtual and override you can’t call the the Base class except from inside the Child class itself (meaning from the Child class methods only). To make this clearer, if you cast the child as Base and call the virtual method, you’ll still get the overriden version not the vertual (original) one. This happens becasue override totally replaces the Base virtual method. It doesn’t just hide it.
Now, if you leave the original Base method with or without the virtual modifier (it doesn’t matter here), and use the new modifier with the Child version of this method, you’ll be hiding the original method only when Child is treated as a Child (Not casted as Base). If you cast it as Base, and call the same method, you’ll get the original version of the method, not the new one.
If you feel confused, check the following code segment:
This code should be explainning itself on its own. I just show that when I call the virtual method ‘View()’ using a Child reference or Base refernce, I always get the overriden version, on the other hand, when I call the ‘ViewNew()’ method using a Base reference, I get the original version. I get the “new” version only when I use a Child reference (or reference of any type that is inherited from Child itself).
This should be the output of that program:
A good remark is that a method can be new and virtual at the same time, just in case you wanted to allow others to override your new method, especially if the old method is not a virtual one ;)
If you are seeking the official word from MSDN, Check MSDN Library > Development Tools and Languages > Visual Studio .NET > Visual Basic and Visual C# > Reference > Visual C# Language > C# Programmer’s Reference > C# Keywords > Operator Keywords > new.
Finally, just tell me if this is too much, causes you confusion, or just never helped you by any means!
Technorati Tags: C#