Click here to Skip to main content
13,258,763 members (45,723 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


3 bookmarked
Posted 18 Apr 2011

Using the New Keyword to shadow a method in a parent class

, 18 Apr 2011
Rate this:
Please Sign up or sign in to vote.
Use of New / Shadowing in C# - Shadowing vs Overriding
We all know that the New keyword is used to shadow a function in the parent class. What it means is that in the child class, the method is treated as if it does not exist in the parent class. The question then arises as to what is the difference between shadowing and overriding? You can as well define a virtual method and override it in the child class. Why do we need New keyword?

The answer is that shadowing helps you "override" a function in a parent method even if that is not marked virtual. In other words, it helps you override a non virtual method in the child class.

A typical use - Consider a class library being used by various modules in a project. There is a method which is being referenced by numerous modules and libraries. You need to make a change in that method but you are not sure if that is going to break the functionality of the modules already referencing that. So you define a class inheriting from the parent class and shadow the method using new keyword. You use your own shadowed method for your logic while not breaking any code due to your change.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
Australia Australia
I am a Senior Software Developer / Technical Consultant in a leading software company.

You may also be interested in...


Comments and Discussions

GeneralGood explanation & practice use. Pin
Tiep Le25-Apr-11 19:59
memberTiep Le25-Apr-11 19:59 
Generalmy vote of 1 Pin
EngleA26-Apr-11 4:42
memberEngleA26-Apr-11 4:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171114.1 | Last Updated 18 Apr 2011
Article Copyright 2011 by anshudutta
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid