Click here to Skip to main content
15,885,921 members
Articles / Programming Languages / C#
Tip/Trick

Using Shadowing in C#

Rate me:
Please Sign up or sign in to vote.
4.39/5 (13 votes)
25 Jul 2011CPOL2 min read 58.3K   8   9
This article shows how you can use shadowing to change the behavior of properties in an inherited class.

Introduction


Shadowing is a useful tool for customizing classes when you want to keep some properties hidden from a third party, but allow your own developers access to specific regions of an inherited class.

Background


My employer specializes in online assessments that evaluate prospective employees for high-potential talent. We allow our clients to set up personality assessments for their prospective employees by passing an Assessment object to our systems through Web Services. When we moved to a new WCF model, we took an opportunity to make our assessment setup more friendly and flexible to our 3rd party developers.

Our new design used two classes: a lightweight assessment class for the customer, and a heavyweight class for internal use. The lightweight class inherited from an interface. The heavyweight class inherited from an abstract class which in turn inherited from the same interface. When the customer sends an assessment, the lightweight assessment class is cast to the heavyweight class and the heavyweight class automatically populates its properties from our database. This hides the database implementation from our third party vendors. In order to avoid future misuse of the heavyweight class, we built it with few set properties and designed it to to be created only by casting from the lightweight class.

But this presented an additional problem. Eventually, I had to get assessment information from our database with a Finished Assessment class. Most of this assessment information reused components from the Heavyweight class. How could I reuse the abstract class when it was absent of set properties? I couldn’t reuse the Heavyweight class since setting up a Heavyweight class required a new assessment.

The answer is shadowing.

Using the Code


Assume you have a section of code in your abstract class that has property which only contains a get, but no set:
public virtual Guid Company_Id { get { return _Company_Id; } }

Theoretically, you should be able to override it in an inherited class by doing this:
public override Guid Company_Id { get { return _Company_Id; } set { _Company_Id = value; } }

Right? Wrong. You can’t override something that isn’t there.

The correct way would be to use the new keyword in place of the override:

public new Guid Company_Id { get { return _Company_Id; } set{ _Company_Id = value; } }

This will allow you to shadow the set property without throwing an exception. You can set up classes that change the behavior of the inherited class by replacing the base member, yet retain the flexibility of the base class so that you can re-use your code.

Points of Interest


The lazy way of doing this would have been to simply add a set command in the abstract class and then override them to take them away in the Heavyweight class. But this could have also opened up tempting holes in the Heavyweight class which would have encouraged programmers to re-use it for all sorts of inappropriate things. With shadowing, the Heavyweight class stays heavy. The Finished Assessment class does its job. And the Lightweight class hides its complicated implementation from our customers.

License

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


Written By
Architect
United States United States
I've been a software developer for over 25 years working on a wide variety of projects. I've written tank simulations, built web software during the .com boom, developed embedded handheld digital cameras, tracked prices of scrap steel and used software to predict where bail jumpers would be hiding.

Comments and Discussions

 
GeneralMy vote of 1 Pin
tbayart14-Dec-18 5:09
professionaltbayart14-Dec-18 5:09 
GeneralMy vote of 3 Pin
prakash_21012-Apr-13 2:12
prakash_21012-Apr-13 2:12 
QuestionExcellent article Pin
Debasish B13-Dec-12 0:05
Debasish B13-Dec-12 0:05 
GeneralMy vote of 4 Pin
Jitendra20053-Nov-12 10:03
Jitendra20053-Nov-12 10:03 
General[My vote of 1] Wrong keywords used. Pin
cythe16-Oct-12 18:16
cythe16-Oct-12 18:16 
Shadowing is used in VB.NET. In C#, we call it "hiding".
GeneralReason for my vote of 1 Not good example. Pin
ROHITH.SUNNY24-Nov-11 20:29
ROHITH.SUNNY24-Nov-11 20:29 
GeneralReason for my vote of 2 New poperty is not virtual. It uses ... Pin
Kelqualyn1-Aug-11 21:00
Kelqualyn1-Aug-11 21:00 
GeneralReason for my vote of 2 It's not really good example. If the... Pin
Đỗ Hồng Ngọc25-Jul-11 18:08
professionalĐỗ Hồng Ngọc25-Jul-11 18:08 
GeneralReason for my vote of 5 Very good, practical example of usin... Pin
DrABELL25-Jul-11 17:29
DrABELL25-Jul-11 17:29 

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.