Is it possible to invoke method A.F() from instance of B class except of using refactoring. Thanks..
class Program
{
public class A
{
public virtual void F()
{
Console.WriteLine( "A" );
}
}
public class B : A
{
public override void F()
{
Console.WriteLine( "B" );
}
}
static void Main( string[] args )
{
B b = new B();
//Here I need Invoke Method A.F() , but not overrode..
Console.ReadKey();
}
}
You may use the new keyword to have another definition of the same (named) method. Depending on the type of reference, you call A's of B's implementation.
public class A
{
public void F()
{
Console.WriteLine( "A" );
}
}
public class B : A
{
public new void F()
{
Console.WriteLine( "B" );
}
}
static void Main( string[] args )
{
B b = new B();
// write "B"
b.F();
// write "A"
A a = b;
a.F();
}
If you feel that new is not the right solution, you should consider to write two methods with a distinguished name.
Generally, you can't call the base implementation from outside the class if the method is properly overridden. This is an OO concept. You must have another method. There are four ways (I can think of) to specify this distinguished method:
new keyword) It's called hiding.new, but based on interface)You need base.F();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With