C# Constructor and its Types – A Detailed Look Definition: In simple terms, Constructor is a special kind of method with class name as method name and gets executed when its (class) object is created. This article will give you a detailed explanation on C# constructors and its types. Now, when we look at the above definition in a broader sense, a constructor is a class method that gets automatically executed whenever class’s object is created or whenever class is initialized. Consider following bit of code: public class MsDotNetHeaven { public MsDotNetHeaven() { //A default Constructor } //Class members } In the above snippet, the method MsDotNetHeaven() is called the constructor of class MsDotNetHeaven, also called default constructor. Behind the scenes What happened behind the scenes : whenever you try to create an object of class or initialize a class, then the default constructor will be automatically i
As most of you know hiding and overriding are two main features based upon inheritance, which is one of the pillars of the OOP. Using these we can redefine a member of the base class in a derived class. But what are the differences. Let me clear this with a simple example. Start a new web application project with a default web form named "WebForm1" using visual studio. Add 2 class to this project with names "BaseClass" and "DerivedClass". The "DerivedClass" should be a subclass of "BaseClass" (i.e. should define like DerivedClass: BaseClass). Define the following functions in the base class named "BaseClass". Notice the keyword "virtual". public virtual string FunctionToOverride() { return "This is from base class FunctionToOverride"; } public string FunctionToHide() { return "This is from base class FunctionToHide"; } Now define th
Comments
Post a Comment