Dynamic and Static binding in C#
Static and dynamic binding are two concepts of programming languages, which are used while working with functions. In overloading and overriding our compiler need to link and bind function and run the program.
So in static binding the function is linked at compile time and so that it’s also called early binding.
In Dynamic binding the function linked at run time and so that it’s also known as late binding.
Static binding can be performed using two possible ways which are following:
- Function overloading
- Operator overloading
In static binding compiler decides which function should run at compile time?
Dynamic binding can be performed using Polymorphism in which we use:
- Function overriding
In this type of binding, function to be called is determined at runtime.
Function Overloading:
In this technique, we have many functions with the same name but different data types and sometimes different numbers of parameters. When we execute the code, the compiler decides at compile time that which function should be called.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
using System; namespace trydemo { class Person { void teacher(int id) { Console.WriteLine("Teachers ID No: {0}", id ); } void teacher(string name) { Console.WriteLine("Teachers Name: {0}" , name); } void teacher(double mobile) { Console.WriteLine("Teachers Mobile No: {0}", mobile); } static void Main(string[] args) { Person p = new Person(); p.teacher(111); p.teacher("Asra Khalid"); p.teacher(03211234567) Console.ReadKey(); } } } |
Function Overriding:
It is much similar to overloading as it also has many functions with the same name but this time, their parameter’s count and data type are also same, but they are defined in different scope.
The function overloading can be done using abstract classes and virtual functions, and the classes must be inherited from each others.
In this technique, we use a concept of object oriented programming called polymorphism, which allow us to call function many times using a different scope, so we have three types of function’s scope in C#.
- Virtual function
- Override function
- New function.
The parent class should use the virtual keyword with his function name so that the child class will able to define the function of the same name within its scope.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
using System; namespace demotry { class dog { public virtual void eat() { Console.WriteLine("Dog eats bones") } } class cat: dog { public override int eat () { Console.WriteLine("Cat eat mouse"); } } class mouse: cat { public override int eat() { Console.WriteLine("Mouse eats cakes"); } } class truedemo { static void Main(string[ ] args) { dog d = new dog(); dog c = new cat(); dog m = new mouse(); d.eat(); c.eat(); m.eat(); Console.ReadKey(); } } } |
See also Function Overloading and overriding in Java