What\'s difference between shadowing and overriding a method in C#?
If there is a case in which you cannot alter the contents of a class, let's say A
, but you want to use its some methods along with you have a method which name is common, you can use your own method implementation by the new
keyword.
The crux point is to use it that both the reference and object must be of the same type.
class A
{
public void Test()
{
Console.WriteLine("base");
}
}
class B : A
{
public new void Test()
{
Console.WriteLine("sub");
}
public static void Main(string[] args)
{
A a = new A();
B aa = new B();
a.Test();
aa.Test();
}
}