Académique Documents
Professionnel Documents
Culture Documents
This article explains the basic differences between the three most frequently used and
confusing keywords in C#. 03 Basics of AngularJS: Part 1
Sample Implementation
New Keyword View All
Virtual and Override Keyword
TECHNOLOGIES
Method ANSWERS+ Riding
Overloading BLOGS VIDEOS INTERVIEWS BOOKS NEWS CHAPTERS CAREER ADVICE JOBS Follow @csharpcorner 14.6K followers
Key Points
Conclusions Find us on Facebook
Overview C# Corner
Like
In this article I'll explain how virtual, override and new keywords vary by taking some set of examples
respectively ﴾some sort of functionality﴿. 87,746 people like C# Corner.
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:
01. // Base Class
02. class A
03. {
04. public virtual void show()
05. {
06. Console.WriteLine("Hello: Base Class!");
07. Console.ReadLine();
08. }
09. }
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:
01. // Base Class
02. class A
03. {
04. public virtual void show()
05. {
06. Console.WriteLine("Hello: Base Class!");
07. Console.ReadLine();
08. }
09. }
10.
11. // Derived Class
12. class B : A
13. {
14. public override void show()
15. {
16. Console.WriteLine("Hello: Derived Class!");
http://www.csharpcorner.com/UploadFile/2072a9/virtualvsoverridevsnewkeywordsincsharp/ 1/7
13/04/2015 Virtual vs Override vs New Keywords in C#
17. Console.ReadLine();
18. }
19. }
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:
01. class A
02. {
03. public void show()
04. {
05. Console.WriteLine("Hello: Base Class!");
06. Console.ReadLine();
07. }
08. }
09.
10. class B : A
11. {
12. public new void show()
13. {
14. Console.WriteLine("Hello: Derived Class!");
15. Console.ReadLine();
16. }
17. }
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:
01. using System;
02. using System.Collections.Generic;
03. using System.Linq;
04. using System.Text;
05.
06. namespace Generics
07. {
08. class A
09. {
10. public void show()
11. {
12. Console.WriteLine("Hello: Base Class!");
13. Console.ReadLine();
14. }
15. }
16.
17. class B : A
18. {
19. public void show()
20. {
21. Console.WriteLine("Hello: Derived Class!");
22. Console.ReadLine();
23. }
24. }
25.
26. class Polymorphism
27. {
28. public static void Main()
29. {
30. A a1 = new A();
31. a1.show();
32. B b1 = new B();
33. b1.show();
34. A a2 = new B();
35. a2.show();
36. }
37. }
38. }
Output Window
http://www.csharpcorner.com/UploadFile/2072a9/virtualvsoverridevsnewkeywordsincsharp/ 2/7
13/04/2015 Virtual vs Override vs New Keywords in C#
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?
Warning Message
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.﴿
Here's a simple snippet of method overriding. So just go through it and guess the output:
01. using System;
02. using System.Collections.Generic;
03. using System.Linq;
04. using System.Text;
05.
06. namespace Generics
07. {
08. class A
09. {
10. public void show()
11. {
12. Console.WriteLine("Hello: Base Class!");
13. Console.ReadLine();
14. }
15. }
16.
17. class B : A
18. {
19. public new void show()
20. {
21. Console.WriteLine("Hello: Derived Class!");
22. Console.ReadLine();
23. }
24. }
25.
26. class Polymorphism
27. {
28. public static void Main()
29. {
30. A a1 = new A();
31. a1.show();
32. B b1 = new B();
33. b1.show();
34. A a2 = new B();
35. a2.show();
36. }
37. }
38. }
http://www.csharpcorner.com/UploadFile/2072a9/virtualvsoverridevsnewkeywordsincsharp/ 3/7
13/04/2015 Virtual vs Override vs New Keywords in C#
Output Window
Explanation
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.
01. using System;
02. using System.Collections.Generic;
03. using System.Linq;
04. using System.Text;
05.
06. namespace Generics
07. {
08. class A
09. {
10. public virtual void show()
11. {
12. Console.WriteLine("Hello: Base Class!");
13. Console.ReadLine();
14. }
15. }
16.
17. class B : A
18. {
19. public override void show()
20. {
21. Console.WriteLine("Hello: Derived Class!");
22. Console.ReadLine();
23. }
24. }
25.
26. class Polymorphism
27. {
28. public static void Main()
29. {
30. A a1 = new A();
31. a1.show();
32. B b1 = new B();
33. b1.show();
34. A a2 = new B();
35. a2.show();
36. }
37. }
38. }
Output Window
http://www.csharpcorner.com/UploadFile/2072a9/virtualvsoverridevsnewkeywordsincsharp/ 4/7
13/04/2015 Virtual vs Override vs New Keywords in C#
Explanation
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.
01. using System;
02. using System.Collections.Generic;
03. using System.Linq;
04. using System.Text;
05.
06. namespace Generics
07. {
08. class A
09. {
10. public virtual void show()
11. {
12. Console.WriteLine("Hello: Base Class!");
13. Console.ReadLine();
14. }
15. }
16.
17. class B : A
18. {
19. public override void show()
20. {
21. Console.WriteLine("Hello: Derived Class!");
22. Console.ReadLine();
23. }
24. }
25.
26. class C : B
27. {
28. public new void show()
29. {
30. Console.WriteLine("Am Here!");
31. Console.ReadLine();
32. }
33. }
34.
35. class Polymorphism
36. {
37. public static void Main()
38. {
39. A a1 = new A();
40. a1.show();
41. B b1 = new B();
42. b1.show();
43. C c1 = new C();
44. c1.show();
45. A a2 = new B();
46. a2.show();
47. A a3 = new C();
48. a3.show();
http://www.csharpcorner.com/UploadFile/2072a9/virtualvsoverridevsnewkeywordsincsharp/ 5/7
13/04/2015 Virtual vs Override vs New Keywords in C#
49. B b3 = new C();
50. b3.show();
51. }
52. }
53. }
Output Window
Explanation
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.
New
Conclusion
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.
Abhishek Jaiswal
A Geek!
Personal Blog: http://geeksangle.ghost.io/
Rank 1m Gold 2
42 Readers Member Times
RELATED ARTICLES
Virtual Method in C# Difference Between Override and New Keyword
Explained Step-by-Step
Difference Between Override and New Keyword Var vs Dynamic Keywords in C#
in C# Method Overriding in C#
Function Overriding and Its Impact During Const vs Readonly in C#
Object Initialization Look at Load Testing in Visual Studio 2012
Differences Among Method Overriding, Method OOPS Concepts and .NET Part 2: Inheritance,
Hiding (new Keyword) and Method Shadowing Abstraction, & Polymorphism
in C#
COMMENTS 5 of 5
good explanation
Gyan Parkash Sep 09, 2014
634 1 0 0 0 Post Reply
nice article
Manju lata Yadav Sep 09, 2014
392 268 62.9k 0 0 Post Reply
Good One....
Divya Sharma Sep 09, 2014
325 389 130.5k 0 0 Post Reply
Thank u guys! :)
Abhishek Jaiswal Sep 09, 2014
42 3.6k 1m 0 0 Post Reply
COMMENT USING
Add a comment...
Comment using...
Facebook social plugin
MVPs MOST VIEWED LEGENDS NOW PRIZES REVIEWS SURVEY CERTIFICATIONS DOWNLOADS Hosted By CBeyond Cloud Services
PHOTOS CODE SNIPPETS CONSULTING TRAINING STUDENTS MEMBERS MEDIA KIT ABOUT US LINKS IDEAS
http://www.csharpcorner.com/UploadFile/2072a9/virtualvsoverridevsnewkeywordsincsharp/ 7/7