No, there is no polymorphism. You have a non-virtual non-virtual method call. Unlike some other languages, methods and properties in C # are not virtual by default.
To demonstrate truly valid polymorphism, you need to:
- Declare a virtual method in a base class
- Use override modifier in derived class
- Use a variable with the compilation time type of the base class to call, but initializing it with an object of a derived type.
, , :
using System;
class Base
{
public virtual void Foo()
{
Console.WriteLine("Base.Foo");
}
}
class Derived : Base
{
public override void Foo()
{
Console.WriteLine("Derived.Foo");
}
}
class Test
{
static void Main()
{
Base x = new Derived();
x.Foo();
}
}