Interviews3 answers
What is Shadowing in C#?
3 Answers
Shadowing is concept to hide functionality provided by base class member. We can also change return type of member also. In overriding we can only change definition but in shadowing we can change return type also. Below is small example
public class ParentClass
{
public int InvoiceNumber = 10;
public virtual void GetInvoice()
{
Console.WriteLine("Invoice from base");
}
}
public class DerivedClass:ParentClass
{
public new string InvoiceNumber = "10";//shadowing
public new string GetInvoice() //shadowing
{
Console.WriteLine("Invoice from Derived");
return string.Empty;
}
}
Dhanik SahniPosted Apr 4, 2014, 10:41 AM EDT
In shadowing, we provide a new implementation to the base class member
without overriding it. We may shadow a base class member in a derived class, by
using the keyword shadows. The access level, return type, and the signature (means
the datatypes of the arguments passed & the order of the types) of the
derived class members which are shadowed, may differ from the base
class.
In C#, we may achieve shadowing using the keyword new. However, when Hiding in C#, the access level, the signature, return type of the derived class must be same as the base class.
http://kalitinterviewquestions.blogspot.com/
In C#, we may achieve shadowing using the keyword new. However, when Hiding in C#, the access level, the signature, return type of the derived class must be same as the base class.
http://kalitinterviewquestions.blogspot.com/
kalit sikkaPosted Jun 17, 2011, 3:15 AM EDT
Shadowing is a concept of polymorphism usage in Object Oriented Programming. This is a concept related to over-riding functions at run-time or making a shadow of the object methods in the inherited classes. Though it is not much used in programming but it may be used sometimes and its usage is very restrictive and unique.
Santosh KumarPosted Jun 7, 2011, 3:34 AM EDT

