Polymorphism in C#

Problem
You want to know about polymorphism in C# along with its two types.

Solution
In general, polymorphism means possessing many forms. In C#, it means one interface, many functions. There are two types of polymorphism in C#. They are compile-time polymorphism and run-time polymorphism. Compile-time polymorphism is also referred to as static or early binding. Run-time polymorphism is also referred to as dynamic or late binding.

Discussion
A practical example of compile-time or static polymorphism is the addition operator. When using the addition operator with numeric data, a mathematical operation occurs. When using the addition operator with string data, concatenation occurs. This example illustrates operator overloading. Method and operator overloading comprise compile-time polymorphism in C#. Abstract classes and virtual methods provide a means for run-time polymorphism. These topics are out of scope for this blog entry. But, the gist is that calls to methods with many implementations get determined at run-time. Let’s say you generate a random number in your code. You have two methods. One gets called when the number is even, and the other gets called when it’s odd. Determining which method gets called is an example of run-time polymorphism.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s