There is a difference, and it lies in the behaviour of objects cast down to
their base classes. For example, let's say you have a class A which has a
virtual method. Class B inherits from A and overrides the virtual method.
If you cast down an object of type B to type A and execute the overridden
method, you will be executing the implementation of the method defined by
class B. However, if the method is not virtual, and it is hidden by a new
method in class B, execution of the method against an object of type B cast
down to type A will result in execution of the class A implementation of the
method. Run the following console application to verify this for yourself:
class ConsoleDemo
{
static void Main()
{
Console.WriteLi ne("Working with a B object:");
B b = new B();
b.ShowOverride( );
b.ShowNew();
Console.WriteLi ne();
Console.WriteLi ne("Working with a B object cast down to an A
object:");
A a = (A)b;
a.ShowOverride( );
a.ShowNew();
Console.ReadLin e();
}
}
class A
{
public virtual void ShowOverride()
{
Console.WriteLi ne("A.ShowOverr ide()");
}
public void ShowNew()
{
Console.WriteLi ne("A.ShowNew() ");
}
}
class B : A
{
public override void ShowOverride()
{
Console.WriteLi ne("B.ShowOverr ide()");
}
public new void ShowNew()
{
Console.WriteLi ne("B.ShowNew() ");
}
}
"news.microsoft .com" <an********@dis cussions.micros oft.com> wrote in message
news:%2******** *******@TK2MSFT NGP11.phx.gbl.. .
Hi,
It is possible to override a non virtual method with the "new" keyword
So how is this different from specifying a method as virtual then
providing the override keyword?
Is there any differences between these two methods of overriding?
Thanks.