The purpose of writing this article is simple; to provide a simple and fresh demonstration of the basic differences between these three frequently used and confusing keywords in C# with some reference example. This article is purely for the beginner in C#.
Outlines
- Overview
- Introduction
- Virtual Keyword
- Override Keyword
- New Keyword
- Demo Examples
- Sample Implementation
- New Keyword
- Virtual and Override Keyword
- Method Overloading + Riding
- Key Points
- Conclusions
Overview
In this article I'll explain how virtual, override and new keywords vary by taking some set of examples respectively (some sort of functionality).
Finally there will be some key points to remember about all these 3 keywords.
Introduction
This introductory part will provide a brief introduction of all these 3 keywords. So here they are.
- Virtual Keyword
The 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:
- // Base Class
- class A
- {
- public virtual void show()
- {
- Console.WriteLine("Hello: Base Class!");
- Console.ReadLine();
- }
- }
- Override Keyword
The 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:
- // Base Class
- class A
- {
- public virtual void show()
- {
- Console.WriteLine("Hello: Base Class!");
- Console.ReadLine();
- }
- }
- // Derived Class
- class B : A
- {
- public override void show()
- {
- Console.WriteLine("Hello: Derived Class!");
- Console.ReadLine();
- }
- }
- New Keyword
The New keyword is also used for polymorphism but in the case of method overriding. So what does overriding means? In simple words we can say that we are changing what the base class does for the derived class.
It is implemented as:
- class A
- {
- public void show()
- {
- Console.WriteLine("Hello: Base Class!");
- Console.ReadLine();
- }
- }
- class B : A
- {
- public new void show()
- {
- Console.WriteLine("Hello: Derived Class!");
- Console.ReadLine();
- }
- }
Demo Example
- Sample Implementation
Here's a simple implementation in C# without using any keywords. Do you think it will run fine, or will it show a runtime or compile-time errors? Let's see:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace Generics
- {
- class A
- {
- public void show()
- {
- Console.WriteLine("Hello: Base Class!");
- Console.ReadLine();
- }
- }
- class B : A
- {
- public void show()
- {
- Console.WriteLine("Hello: Derived Class!");
- Console.ReadLine();
- }
- }
- class Polymorphism
- {
- public static void Main()
- {
- A a1 = new A();
- a1.show();
- B b1 = new B();
- b1.show();
- A a2 = new B();
- a2.show();
- }
- }
- }
Output Window

It is showing some sort of output. That means there is neither a runtime nor a compile-time error. But it will definitely show a warning in Visual Studio. So do you want to know what it is and how to remove it?
Keep reading and you will go through that.
Warning Message
Here's the warning message that you will get:
Solution
The solution of this problem is already in the warning. Just read it carefully and you will get that. Yes you got that right, for removing that warning we need to use the new keyword.
In the next sample demo example is showing you a simple demo snippet and implementation of the new keyword. (I hope now you will be getting why we are using the new keyword in here.) - New keyword | Method Overhiding
Here's a simple snippet of method overriding. So just go through it and guess the output:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace Generics
- {
- class A
- {
- public void show()
- {
- Console.WriteLine("Hello: Base Class!");
- Console.ReadLine();
- }
- }
- class B : A
- {
- public new void show()
- {
- Console.WriteLine("Hello: Derived Class!");
- Console.ReadLine();
- }
- }
- class Polymorphism
- {
- public static void Main()
- {
- A a1 = new A();
- a1.show();
- B b1 = new B();
- b1.show();
- A a2 = new B();
- a2.show();
- }
- }
- }
Output Window
Explanation
The procedure goes something like that:
- Virtual and Override Keywords | Method Overriding
This is a simple example of method overriding. Just go through it and guess the output again and also try to differentiate between the previous snippet and this snippet.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace Generics
- {
- class A
- {
- public virtual void show()
- {
- Console.WriteLine("Hello: Base Class!");
- Console.ReadLine();
- }
- }
- class B : A
- {
- public override void show()
- {
- Console.WriteLine("Hello: Derived Class!");
- Console.ReadLine();
- }
- }
- class Polymorphism
- {
- public static void Main()
- {
- A a1 = new A();
- a1.show();
- B b1 = new B();
- b1.show();
- A a2 = new B();
- a2.show();
- }
- }
- }
Output Window
Explanation
The flow goes something like that:
- Overriding + Hiding | Together
This snippet shows how both of these methods can work together in a same snippet. So just go through this and guess the output.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace Generics
- {
- class A
- {
- public virtual void show()
- {
- Console.WriteLine("Hello: Base Class!");
- Console.ReadLine();
- }
- }
- class B : A
- {
- public override void show()
- {
- Console.WriteLine("Hello: Derived Class!");
- Console.ReadLine();
- }
- }
- class C : B
- {
- public new void show()
- {
- Console.WriteLine("Am Here!");
- Console.ReadLine();
- }
- }
- class Polymorphism
- {
- public static void Main()
- {
- A a1 = new A();
- a1.show();
- B b1 = new B();
- b1.show();
- C c1 = new C();
- c1.show();
- A a2 = new B();
- a2.show();
- A a3 = new C();
- a3.show();
- B b3 = new C();
- b3.show();
- }
- }
- }
Output Window
Explanation
The flow goes something like that:
Key Points
I am providing some key points about these keywords by taking a reference of method overloading and overriding concepts, since these keywords are used in these mechanisms.
Virtual and Override
- Used in polymorphism implementation
- Includes same method name and same params
- Used in method overriding
- It is also called runtime polymorphism
- Causes late binding
New
- It is also used in polymorphism concept
- Includes the same method name and different params
- Used in method overriding
- It is compile-time polymorphism
- Causes early binding
Conclusion
So did you like it, I hope you so!
Well I tried to provide just a brief sketch of these keywords, that are very necessary for a beginner in C# as well as in OOP. If you have a good understaind of OOP then you can take any object-oriented language in your pocket.
So just go through OOP first and then C# and if you are facing any problem then feel free to ping or message me.
Happy coding, Cheers!

Sanjay YadavPosted Jul 7, 2018, 11:51 PM
I think new key is only used to hide warning.....
Satish RathorePosted Aug 18, 2017, 5:42 AM
Nice explanation..
nikhil kapoorPosted May 23, 2017, 3:23 AM
Why should we use new , instead of virtual ! any real life example please ?
Abhishek JaiswalPosted Aug 7, 2016, 6:40 AM
Thanks man, i'll surely take a look. Cheers! :)
ehsan soltaniPosted Jul 26, 2016, 4:00 AM
Thanks for your attempts. For more information about Virtual Methods check this : http://www.projectpardaz.ir/?????????-???-?????-??-????/
Ramakrishna BasagallaPosted Apr 26, 2016, 1:06 AM
Good One
Abhishek JaiswalPosted Sep 9, 2014, 11:52 AM
Thank u guys! :)
Divya SharmaPosted Sep 9, 2014, 4:10 AM
Good One....
Manju lata YadavPosted Sep 9, 2014, 2:04 AM
nice article
Dinesh BeniwalPosted Sep 9, 2014, 1:15 AM
Good work Abhishek.
Gyan ParkashPosted Sep 9, 2014, 1:04 AM
good explanation