Versioning with the Override and New Keywords (C# Programming Guide) 
 

The C# language is designed so that versioning between base and derived classes in different libraries can evolve and maintain backwards compatibility. This means, for example, that the introduction of a new member in a base class with the same name as a member in a derived class is completely supported by C# and does not lead to unexpected behavior. It also means that a class must explicitly state whether a method is intended to override an inherited method, or whether a method is a new method that simply hides a similarly named inherited method.

C# allows derived classes to contain methods with the same name as base class methods.

  • The base class method must be defined virtual.
  • If the method in the derived class is not preceded by new or override keywords, the compiler will issue a warning and the method will behave as if the new keyword were present.
  • If the method in the derived class is preceded with the new keyword, the method is defined as being independent of the method in the base class.
  • If the method in the derived class is preceded with the override keyword, objects of the derived class will call that method rather than the base class method.
  • The base class method can be called from within the derived class using the base keyword.
  • The override, virtual, and new keywords can also be applied to properties, indexers, and events.

By default, C# methods are not virtual if a method is declared as virtual, any class inheriting the method can implement its own version. To make a method virtual, the virtual modifier is used in the method declaration of the base class. The derived class can then override the base virtual method by using the override keyword or hide the virtual method in the base class by using the new keyword. If neither the override keyword nor the new keyword is specified, the compiler will issue a warning and the method in the derived class will hide the method in the base class. For more information, see Compiler Warning CS0108.

To demonstrate this in practice, assume for a moment that Company A has created a class entitled GraphicsClass, which your program uses. GraphicsClass looks like this:

My note :

A virtual method is used to over-ride the data in Base class with the data by a method in the derived class. With out virtual key word, the data in the base class remain unaffected,

example virtual methods
//no_virtual_3.cs
using System;
namespace team_players_no_virtual
{
class team
{
public void show() { Console.WriteLine("Welcome to Team Bdale"); }
}

class player_1 : team
{
public void show() { Console.WriteLine("player_1 in defense"); }
}

class Test
{
static void Main(string[] args)
{
team a = new team();;
player_1 b = new player_1();
a.show();
b.show();
a = new player_1();
a.show();
}
}
}

 
//with_virtual_3.cs
using System;
namespace team_players_with_virtual
{
class team
{
public virtual void show() { Console.WriteLine("Welcome to Team Bdale"); }
}

class player_1 : team
{
public override void show() { Console.WriteLine("player_1::defense"); }
}

class Test
{
static void Main(string[] args)
{
team a = new team();
player_1 b = new player_1();;
a.show();
b.show();
a = new player_1();
a.show();
}
}
}
 

C:\MySharp\virtual>csc no_virtual_3.cs
Microsoft (R) Visual C# .NET Compiler version 7.10.6001.4
for Microsoft (R) .NET Framework version 1.1.4322
Copyright (C) Microsoft Corporation 2001-2002. All rights reserved.

no_virtual_3.cs(12,27): warning CS0108: The keyword new is required on
'team_players_no_virtual.player_1.show()' because it hides inherited
member 'team_players_no_virtual.team.show()'
no_virtual_3.cs(7,27): (Location of symbol related to previous warning)


C:\MySharp\virtual>no_virtual_3
Welcome to Team Bdale
player_1 in defense
Welcome to Team Bdale

 
 
Now if you note that the reassignment did work (above red letters)

a = new player_1();
a.show();

On the other hand virtual key word allowed override the reassignment

Note at the run time

example

//no_virtual_3b.cs
using System;
namespace team_players_no_virtual
{
class team
{
public void show() { Console.WriteLine("Welcome to Team Bdale"); }
}

class player_1 : team
{
public void show() { Console.WriteLine("player_1 in defense"); }
}

class Test
{
static void Main(string[] args)
{
team a = new team();;
player_1 b = new player_1();
a.show();
b.show();
a = b;
a.show();
}
}
}
 

 

//with_virtual_3b.cs
using System;
namespace team_players_with_virtual
{
class team
{
public virtual void show() { Console.WriteLine("Welcome to Team Bdale"); }
}

class player_1 : team
{
public override void show() { Console.WriteLine("player_1::defense"); }
}

class Test
{
static void Main(string[] args)
{
team a = new team();
player_1 b = new player_1();;
a.show();
b.show();
a = b;
a.show();
}
}
}