User Name:

User Email:

This information will only be saved for the purposes of communicating with those who have provided this information voluntarily regarding our services.We will never sell your name or email address to anyone.
© 2021 - First Crazy Developer (Abhishek Kumar)

crazydeveloper What is the use of Shadowing in C#

The concept of Shadowing is not a part of object oriented programming principle but this keyword is so important with classes & objects in C#. We must know the behaviour of Shadowing in C#.

Before going to discuss about Shadowing, we write some code and understand the base concept. Now let’s take a look at the following code:

  1. public class BaseClass
  2. {
  3. public int addTotal = 0;
  4. }
  5. public class ChildClass : BaseClass
  6. {
  7. int num1 = 10;
  8. int num2 = 20;
  9. int total = 0;
  10. public void addTotal()
  11. {
  12. total = num1 + num2;
  13. }
  14. }

In the above code we can see there's a class called "BaseClass". We then create one more class called "ChildClass" inherit from "BaseClass". So now we have concreate class "ChildClass" with the parent class "BaseClass". In "BaseClass" we defined a simple integer type variable called "addTotal". For a good reason or a bad reason in "ChildClass" we defined a method called "addTotal" with return type void.

Now the scenario is parent class has a variable called "addTotal" and child class has a method called "addTotal".

Here many developer will be thinking that above code will be compiled?

Now first we compile the above code.

What we get? The above code build succeeded. Now we can say above code is valid statement in C#.

Now using this valid statement as a base we write some more code, but first, here’s an important question:

We know that both parent and child class have their own defination for "addTotal", but when we will create the object of child class will point which defination?

Let’s see what happens when we try this:

  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. BaseClass objBaseClass = new BaseClass();
  4. ChildClass objChildClass = new ChildClass();
  5. objBaseClass.addTotal = 10;
  6. objChildClass.addTotal();
  7. }

In above code we can see there are an object of parent class called "objBaseClass" and child class called "objChildClass". Now when we used "objBaseClass.addTotal",  

we can see that this point to integer type variable and "objChildClass.addTotal" point to void type method and don't have any reference of integer type variable.

Actually when we use parent, "addTotal" is variable and with child "addTotal" has been converted to a method. This is term as "Shadowing in C#".

In Shadowing the parent class element is completely replace by the child class in all terms. But here again the important question: What is the use of Shadowing?

Because definitely, it is confusing when the completely element type replaced. In example we just saw the variable "addTotal" converted to the method. As per developer view "this is not a good practice in programing language". If Shadowing is going to confuse developer, If Shadowing is going to bring bad coding practice, the next question arises is "why we do need Shadowing then"?

Now so in order to understand Shadowing much better, we change the requirements. Now, analyse the following code:

  1. public class Sales
  2. {
  3. public int InvoiceNumber { set; get; }
  4. }

Now, we have "Sales" class which has an integer type property called "InvoiceNumber". This is rock star class used many place in our business to accept sales invoice number.  Let assumes we get new request or requirement, where invoice number should also accept alphanumeric values. Here, we really can't change the datatype of "InvoiceNumber"  from integer to string, because if we change this property our current client who are consuming this class will be not happy about it. So, what we do for it to implement this change without effected previous defined class. Here, we can't use override, because override only use to change the implementation, doesn't change the datatype. Here exactly demand the Shadowing with this requirement. Now, look the following code:

  1. public class Sales
  2. {
  3. public int InvoiceNumber { set; get; }
  4. }
  5. public class SalesVersion2 : Sales
  6. {
  7. public object InvoiceNumber { set; get; }
  8. }

In the above code we can see there's a one more class called "SalesVersion2" inherit from "Sales". We also change the datatype of property of "InvoiceNumber" from integer to object. So now this property can use with any datatype like integer or alphanumeric. Here we achieved old clients happy with "Sales" class and new clients can use "SalesVersion2" class with their new requirements and also kept the vocabulary same but datatype changed.

But when we compiled the above code we get warning from compiler:

In the above image we can see that waring, where compiler inform us to use the "new" keyword if hiding is indented. Now, look the following code:

  1. public class Sales
  2. {
  3. public int InvoiceNumber { set; get; }
  4. }
  5. public class SalesVersion2 : Sales
  6. {
  7. public new object InvoiceNumber { set; get; }
  8. }

In the above code we can see there's new keyword used for implementing the Shadowing. Here "new" keyword actually says to compiler that, yes I know what I am doing as per developer, go and hide the parent class functionality and replace with new definition.

Before going to end this discussion, one important difference clear that between "Shadowing and Overriding".

Shadowing complete replaces the element of the parent, while Overriding only replace the implementation of parent elements.

Important note: - If you have option to use virtual methods and overrides then go with that. Shadowing concept break the rule of SOLID principle as well as Design Pattern Techniques. Shadowing concept is bad coding practice for coder.

Happy reading!

crazydeveloper Home Page 31 July 2015

Become a Fan