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. namespace NewVirtualOverride
  7. {
  8. // Base Class
  9. class Parent
  10. {
  11. public virtual void SayHello()
  12. {
  13. Console.WriteLine("Hello from Parent function!!!");
  14. }
  15. }
  16. class Child : Parent
  17. {
  18. public override void SayHello()
  19. {
  20. Console.WriteLine("Hello from Child class function!!!");
  21. }
  22. }
  23. class Program
  24. {
  25. static void Main(string[] args)
  26. {
  27. Parent pObj = new Parent();
  28. pObj.SayHello();
  29. Child cObj = new Child();
  30. cObj.SayHello();
  31. Parent pcObj = new Child();
  32. pcObj.SayHello();
  33. Console.ReadLine();
  34. }
  35. }
  36. }
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. namespace NewVirtualOverride
  7. {
  8. // Base Class
  9. class Parent
  10. {
  11. public void SayHello()
  12. {
  13. Console.WriteLine("Hello from Parent function!!!");
  14. }
  15. }
  16. class Child : Parent
  17. {
  18. public new void SayHello()
  19. {
  20. Console.WriteLine("Hello from Child class function!!!");
  21. }
  22. }
  23. class Program
  24. {
  25. static void Main(string[] args)
  26. {
  27. Parent pObj = new Parent();
  28. pObj.SayHello();
  29. Child cObj = new Child();
  30. cObj.SayHello();
  31. Parent pcObj = new Child();
  32. pcObj.SayHello();
  33. Console.ReadLine();
  34. }
  35. }
  36. }
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

New

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#: