What is difference between method hiding and shadowing in c#. [Resolved]

Posted by Allemahesh under C# on 11/29/2013 | Points: 10 | Views : 2350 | Status : [Member] [MVP] | Replies : 6
Can any one tell me what is difference between method hiding and shadowing in c#.




Responses

Posted by: Allemahesh on: 12/6/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down

Resolved
Finally I got the answer for my questions and I have written an article on the same.

Link:-
http://www.c-sharpcorner.com/UploadFile/8911c4/different-between-method-overriding-method-hiding-new-keyw/

Allemahesh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: vishalneeraj-24503 on: 11/29/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Please find below URLs:-
http://www.youtube.com/watch?v=c7bH8gjwtHo
http://stackoverflow.com/questions/392721/difference-between-shadowing-and-overriding-in-c
http://stackoverflow.com/questions/9520226/what-is-the-difference-between-method-hiding-and-shadowing-in-c
http://stackoverflow.com/questions/11046387/exact-difference-between-overriding-and-hiding

Hope this helps you.


Allemahesh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: vishalneeraj-24503 on: 11/29/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Also check below URLs:-
http://social.msdn.microsoft.com/Forums/vstudio/en-US/0a7b9386-93e7-49b8-8781-c06a16c2941c/what-is-the-oops-concept-of-shadowing-in-c?forum=csharpgeneral
http://dotnetinterviewcracker.blogspot.in/2012/10/differences-between-shadowing-and.html
http://www.codeproject.com/Questions/188024/Difference-between-Overriding-Method-Shadowing

Allemahesh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Samirbhogayta on: 12/4/2013 [Member] Starter | Points: 25

Up
0
Down
hi.. this is your answer

Shadowing is another commonly used term for hiding. The C# specification only uses "hiding" but either is acceptable.

You call out just "method hiding" but there are forms of hiding other than method hiding. For example:
namespace N
{
class D {}
class C
{
class N
{
class D
{
N.D nd; // Which N.D does this refer to?

the nested class N hides the namespace N when inside D.


Can we call them as polymorphism (compile time or run time)?

Method hiding can be used for polymorphism, yes. You can even mix method hiding with method overriding; it is legal to introduce a new virtual method by hiding an old virtual method; in that case which virtual method is chosen depends on the compile-time and run-time type of the receiver. Doing that is very confusing and you should avoid it if possible.


SAMIR
Sr. Software Engineer

Allemahesh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kmandapalli on: 12/4/2013 [Member] Silver | Points: 25

Up
0
Down
Hi,

They are just two different words for the same thing, but differ in the context where you most often use them. Typically, what is called "hiding" is related to polymorphism but what is called "shadowing" is not.

In C# parlance, when you say "hiding" you're usually talking about inheritance, where a more derived method "hides" a base-class method from the normal inherited method call chain.

When you say "shadow" you're usually talking about scope: an identifier in an inner scope is "shadowing" an identifier at a higher scope. In other languages, what is called "hiding" in C# is sometimes called "shadowing" as well.

Both are compile-time concepts; they describe what object a given identifier refers to in a given context when the compiler goes to bind it.

public class A
{
public int B;
public void C()
{
return this.B;
}
}

public class D : A
{
public int X;

public new void C()
{
var X = 1.0m;
return X;
}
}

Method D.C() "hides" method A.C(); normally, a call to D.C() would always call into the base classes A.C() method, since it's not virtual. We don't want that; we want D.C(). Obviously this is something you should try to avoid, because it's confusing, especially if you start up-casting your D's to A's, but it exists if you need it. Also, note that method hiding is automatic: without the new keyword here, D.C() still hides A.C() but we get a warning because usually that's not what you want. The new keyword just makes it clear that is really is what we want.

Local variable X in D.C() shadows class member D.X within the scope of D.C() only. In this case, there are two things in scope that could legitimately be called X and the compiler needs rules to tell it which one you mean. The "more local" X shadows the "less local" D.X so that's what we get.


Mark as answer if satisfied...


Regards,
Shree M.

Kavya Shree Mandapalli

Allemahesh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ring on: 12/6/2013 [Member] Starter | Points: 25

Up
0
Down
The VB.NET compiler calls it shadowing, in C# it is called hiding. Calling it shadowing in C# is a spill-over from VB.

Allemahesh, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response