EDN Admin
Well-known member
<p align=left><font face=Arial size=2>Hola!</font>
<p align=left>
<p align=left><font face=Arial size=2>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 dont see any difference </font>(apart from the fact that only virtual methods in base class can be overridden in derived class) in the following program:
<p align=left>
using System;
<p align=left>
class BaseClass
{
public virtual string overridingMethod()
{
return "BaseClass overridingMethod n";
}
public string hidingMethod()
{
return "BaseClass hidingMethod n";
}
}
<p align=left>
class DerivedClass : BaseClass
{
public override string overridingMethod()
{
return "DerivedClass overridingMethod n";
}
public new string hidingMethod()
{
return "DerivedClass hidingMethod n";
}
}
<p align=left>
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());
}
}
View the full article
<p align=left>
<p align=left><font face=Arial size=2>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 dont see any difference </font>(apart from the fact that only virtual methods in base class can be overridden in derived class) in the following program:
<p align=left>
using System;
<p align=left>
class BaseClass
{
public virtual string overridingMethod()
{
return "BaseClass overridingMethod n";
}
public string hidingMethod()
{
return "BaseClass hidingMethod n";
}
}
<p align=left>
class DerivedClass : BaseClass
{
public override string overridingMethod()
{
return "DerivedClass overridingMethod n";
}
public new string hidingMethod()
{
return "DerivedClass hidingMethod n";
}
}
<p align=left>
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());
}
}
View the full article