Virtual, Override And New Keywords In C#

The purpose of writing this article is to explain the basic differences between the frequently used keywords in C#. This article is mainly targeted for the beginner to .NET technologies.

Overview

In this article I am going to explain how virtual, override and new keywords vary by taking some sets of examples respectively.

Virtual Keyword

Virtual keyword is used for generating a virtual path for its derived classes on implementing method overriding. The Virtual keyword is used within a set with an override keyword. It is used as:

  1. class Parent  
  2. {  
  3.     public virtual void SayHello()  
  4.     {  
  5.         Console.WriteLine("Hello from Parent function!!!");  
  6.         Console.ReadLine();  
  7.     }  
  8. }  
Override Keyword

Override keyword is used in the derived class of the base class in order to override the base class method. The Override keyword is used with the virtual keyword, as in:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace NewVirtualOverride  
  8. {  
  9.     // Base Class    
  10.     class Parent  
  11.     {  
  12.         public virtual void SayHello()  
  13.         {  
  14.             Console.WriteLine("Hello from Parent function!!!");  
  15.         }  
  16.     }  
  17.   
  18.     class Child : Parent  
  19.     {  
  20.         public override void SayHello()  
  21.         {  
  22.             Console.WriteLine("Hello from Child class function!!!");  
  23.   
  24.         }  
  25.     }  
  26.   
  27.     class Program  
  28.     {  
  29.         static void Main(string[] args)  
  30.         {  
  31.             Parent pObj = new Parent();  
  32.             pObj.SayHello();  
  33.             Child cObj = new Child();  
  34.             cObj.SayHello();  
  35.             Parent pcObj = new Child();  
  36.             pcObj.SayHello();  
  37.   
  38.   
  39.             Console.ReadLine();  
  40.         }  
  41.     }  
  42. }  
output

New Keyword

New keyword is used for the runtime polymorphism (method overriding), we can change the base class function with derived class function.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace NewVirtualOverride  
  8. {  
  9.     // Base Class    
  10.     class Parent  
  11.     {  
  12.         public void SayHello()  
  13.         {  
  14.             Console.WriteLine("Hello from Parent function!!!");  
  15.         }  
  16.     }  
  17.   
  18.     class Child : Parent  
  19.     {  
  20.         public new void SayHello()  
  21.         {  
  22.             Console.WriteLine("Hello from Child class function!!!");  
  23.   
  24.         }  
  25.     }  
  26.   
  27.     class Program  
  28.     {  
  29.         static void Main(string[] args)  
  30.         {  
  31.             Parent pObj = new Parent();  
  32.             pObj.SayHello();  
  33.             Child cObj = new Child();  
  34.             cObj.SayHello();  
  35.             Parent pcObj = new Child();  
  36.             pcObj.SayHello();  
  37.   
  38.   
  39.             Console.ReadLine();  
  40.         }  
  41.     }  
  42. }  
output

In the above example if you don't specify the ‘new’ keyword it will give warning message as we have already specified the same method name in the parent class, but it will execute the child class method by default.

error
Key Points

Below are the key points on new, virtual, override keywords.

Virtual and Override

 

  • Used in polymorphism implementation (method overriding)
  • It is also called runtime polymorphism
  • Includes same method name and same signatures
  • Causes late binding

New

  • It is also in polymorphism implementation (method overriding)
  • Includes the same method name and different signatures
  • It is compile-time polymorphism
  • Causes early binding

Hope this article will help you in understanding how to implement runtime polymorphism and also concepts of late and early binding.

Read more articles on C#:

Up Next
    Ebook Download
    View all
    Learn
    View all