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 happen...
Delegates in C# Delegates in C# are similar to functional pointers in C++ and are used to encapsulate a method that have similar signature like delegate. We can declare a delegate type with “delegate” keyword in C#. Moving forward, we will see more about delegates and its uses. Delegates – A Detailed Look A delegate is a type that references a method once it is assigned to that method and it behaves exactly like that method. The delegate method can be used like any other method, with parameters and return value. Any method that matches the delegate’s signature, in terms of return type and parameters can be assigned to that delegate. Some information about Delegates 1. Delegates have are similar to c++ function pointer but are type safe. 2. Delegates allow method to be passed as parameters. 3. Delegates can be used to define callback methods. 4. ...
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 ...
Comments
Post a Comment