The C# Modifier ‘new’

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 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 LibraryDevelopment Tools and LanguagesVisual Studio .NETVisual Basic and Visual C#ReferenceVisual C# LanguageC# Programmer’s ReferenceC# KeywordsOperator 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#,CSharp,OOP
Share With Friends:

P.S. Please help me out by checking this offer, then look below for a small Thank You.

How did I learn that?

As a bonus for coming here, I'm giving away a free newsletter for web developers that you can sign up for from here.
It's not an anything-and-everything link list. It's thoughtfully collected picks of articles and tools, that focus on Angular 2, ASP.NET 5, and other fullstack developer goodies.

  • You should probably use an ASP.NET example instead of a Windows Console application since your are bloggin from Just a suggestion.

  • First, this is port of an old post. I’ve stated that above!!!

    Also, for such language stuff, the Console is the best to explain this stuff. That’s what most instructors claim anyway :).

    “ince your are bloggin from”
    I’ve contacted Joe about whether I can even post personal stuff here. He said it is OK. I really hope it is, and I wasn’t mistaken by porting my stuff here.

  • http://

    Good one, thanks.

  • Rob

    I agree totally … that a Console Application is by far the best way to demonstrate the principles that you’re explaining. Overloading the sample with a load of “cruft” would take away from the ease of understanding.

    Although – a good example of this would be a custom control that inherits from one provided by the framework, perhaps? :)

  • Oh yeah, that example would pretty much help keep the reader’s focus on the article’s point best :D :C :)

  • http://

    Yes its good example to eliminate confusions… and i suggest console is the right option as most of MSDN also based on it