recherche

Hola!

What is the oops concept of shadowing in C# Is it the same as hiding, using 'new' keyword How do you choose between 'override' and 'new', as I don't see any difference (apart from the fact that only virtual methods in base class can be overridden in derived class) in the following program:

using System;

class BaseClass
{
public virtual string overridingMethod()
{
return "BaseClass overridingMethod \n";
}

public string hidingMethod()
{
return "BaseClass hidingMethod \n";
}
}

class DerivedClass : BaseClass
{
public override string overridingMethod()
{
return "DerivedClass overridingMethod \n";
}

public new string hidingMethod()
{
return "DerivedClass hidingMethod \n";
}
}

class MainClass
{
static void Main()
{
BaseClass bc = new BaseClass();

Console.WriteLine(bc.overridingMethod());

Console.WriteLine(bc.hidingMethod());

DerivedClass dc = new DerivedClass();

Console.WriteLine(dc.overridingMethod());

Console.WriteLine(dc.hidingMethod());
}
}



Re: Visual C# General What is the oops concept of shadowing in C#?

Chad Moran

If you make a member virtual it means it can be overrridden and still b able to be usable by the derrived class using the override keyword. Using the new keyword actually replaces the base member and you cannot use base.MemberName. With a virtual overriden member you can still access the base implementation.

Hope this helps!






Re: Visual C# General What is the oops concept of shadowing in C#?

timvw

Imho, the following code snippet might help you understanding the concept of virtual/overriding and hiding/new better.

Use an instance of DerivedClass in the context of a BaseClass:

Code Snippet

BaseClass bc2 = dc;
Console.WriteLine(bc2.overridingMethod());
Console.WriteLine(bc2.hidingMethod());







Re: Visual C# General What is the oops concept of shadowing in C#?

recherche

Cool! But what is shadowing in C#





Re: Visual C# General What is the oops concept of shadowing in C#?

timvw

The concept of "Hiding" in c# is the same as the concept of "Shadowing" in vb.net.