This article explains the basic differences between the three most frequently used and confusing keywords in C#.
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
OverviewIn 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.IntroductionThis introductory part will provide a brief introduction of all these 3 keywords. So here they are.
Demo Example
Output WindowIt 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 MessageHere's the warning message that you will get:
SolutionThe 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.)
ExplanationThe procedure goes something like that:
Key PointsI 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
New
ConclusionSo 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!
Programming C# for Beginners