Вы находитесь на странице: 1из 2

Polymorphism in C#

Polymorphism is a Greek word, meaning "one name many forms". In other words, one object has many
forms or has one name with multiple functionalities. "Poly" means many and "morph" means forms.
Polymorphism provides the ability to a class to have multiple implementations with the same name. It is
one of the core principles of object oriented Programming after encapsulation and inheritance.

Types of Polymorphism
There are two types of polymorphism in C#:
 Static / Compile Time Polymorphism.
 Dynamic / Runtime Polymorphism.

Static or Compile Time Polymorphism

It is also known as Early Binding. Method overloading is an example of Static Polymorphism. In
overloading, the method / function has a same name but different signatures. It is also known as Compile
Time Polymorphism because the decision of which method is to be called is made at compile time.
Overloading is the concept in which method names are the same with a different set of parameter.
Dynamic / Runtime Polymorphism
Dynamic / runtime polymorphism is also known as late binding. Here, the method name and the method
signature (number of parameters and parameter type must be the same and may have a different
implementation). Method overriding is an example of dynamic polymorphism.
Method overriding can be done using inheritance. With method overriding it is possible for the base class
and derived class to have the same method name and same something. The compiler would not be aware
of the method available for overriding the functionality, so the compiler does not throw an error at
compile time. The compiler will decide which method to call at runtime and if no method is found then it
throws an error.
What is a constructor in C#?
A special method of the class that is automatically invoked when an instance of the class is created is
called a constructor. The main use of constructors is to initialize private fields of the class while creating
an instance for the class. When you have not created a constructor in the class, the compiler will
automatically create a default constructor of the class. The default constructor initializes all numeric fields
in the class to zero and all string and object fields to null.
Some of the key points regarding constructor are
 A class can have any number of constructors
 A constructor doesn't have any return type, not even void.
 A static constructor can not be a parametrized constructor.
 Within a class you can create one static constructor only.
In C#, constructors can be divided into 4 types
 Default Constructor
 Parametrized Constructor
 Copy Constructor
 Private Constructor
Default Constructor in C#
A constructor without any parameters is called a default constructor; in other words this type of
constructor does not take parameters. The drawback of a default constructor is that every instance of the
class will be initialized to the same values and it is not possible to initialize each instance of the class with
different values. The default constructor initializes:
 All numeric fields in the class to zero.
 All string and object fields to null
Parameterized Constructor in C#
A constructor with at least one parameter is called a parametrized constructor. The advantage of a
parametrized constructor is that you can initialize each instance of the class with a different value.
Copy Constructor in C#
The constructor which creates an object by copying variables from another object is called a copy
constructor. The purpose of a copy constructor is to initialize a new instance to the values of an existing
Private Constructor in C#
When a constructor is created with a private specifier, it is not possible for other classes to derive from
this class, neither is it possible to create an instance of this class. They are usually used in classes that
contain static members only. Some key points of a private constructor are:
 One use of a private constructor is when we have only static members.
 It provides an implementation of a singleton class pattern
 Once we provide a constructor that is either private or public or any, the compiler will not add the
parameter-less public constructor to the class.