Early Binding and Late Binding Step by Step

Introduction

Friends..! Welcome to Learn C Sharp Step by Step Tutorial site which covers everything related to c#. Here you can learn C# through our tutorial articles, videos and other sources.

If you want to know more indepth C# along with MVC, Sharepoint, AngularJS, Object oriented programming, ASP.NET and many more then visit our pricing page to get Complete CSharp + .NET Tutorial Video Pack and #Questpond subscription.

Questpond is an online platform and as well as offline platform to learn .NET technologies. To know about #Questpond you can check our website, facebook page and youtube.

Feel free to contact us if you have any query or doubt.

Now coming back to our session, In this article session we are going to know about Early binding and Late Binding step by step in C#.

When C# compiler on .NET Framework performs binding only when an object is been assigned to a object variable of a specific type.

  • 1. Early Binding
  • 2. Late Binding

These are two type of bindings performs by C# compiler on .NET framework (while using C# language).

Early Binding

Early binding means methods, properties are detected and checked during compile time.Here compiler knows about what kind of object it is. If a method or property does not exist or has a data type problems then the compiler automatically throws an exception during compile time.

Let's summarize, Methods and properties which are detected and checked during compile time is called as early binding.

Here objects are strongly typed objects or static objects. Due to this early binding improves the performance and ease of development. Application runs faster since there is no-type cast in early binding.

Early binding reduces the number of severity of run-time errors upfront before going it to runtime.

So, now let's see a real time example based on Early binding.


   class clsEstateAgent
    {
        private int _agentcode = 0;
        private string _agentname = "";

        public int AgentCode
        {
            set
            {
                _agentcode = value;
            }
            get
            {
                return _agentcode;
            }
        }

        public string AgentName
        {
            set
            {
                _agentname = value;
            }
            get
            {
                return _agentname;
            }
        }

        public void AddAgent()
        {
            Console.WriteLine("Adding Estate Agent...!");
        }

        public void UpdateAgent()
        {
            Console.WriteLine("Updating Estate Agent...!");
        }

    }

In this example we have created a simple class "clsEstateAgent" with some property and method as shown in above code. Now we will create an object of this class (strong type) and since compiler will know the kind of object so it should show an error if we try to call unavailable method. So let's check it.

	class Program
    {
        static void Main(string[] args)
        {
            clsEstateAgent obj = new clsEstateAgent();
            obj.AgentCode = 1001;
            obj.AgentName = "Khadak";
            obj.AddAgent();
            obj.DeleteAgent();
        }
    }

Here from above code you can see that there is no "DeleteAgent" method written in the class code so hence it should throw an error by compiler during compile time. So compile and check it.

As you saw friends, Hence it is proved that for strong types / static types .NET compiler automatically detected an compile-time error and display's error output.

Late Binding

When an object is dynamic or not known which will only bind during runtime is called as Late binding. It is just an opposite of early binding. In late binding compilation, compiler does not know what kind of object or actual type of an object which contains methods & properties so it bypass the compile-time checking which was handled by run-time checking. Runtime handling or Late Binding objects are of dynamic types means actual type of an object is decided depending on data on right hand site which is possible dynamically during runtime only.

Things we can handle statically is done at early binding and things which can handled during dynamically only is done at late binding. I would suggest best example for late binding will be reflection and dynamic keyword.

So let's see a simple example on late binding.

 class Program
    {
        static void Main(string[] args)
        {
            dynamic DynObj = 4;
            Console.WriteLine(DynObj.GetType());

        }
    }
 

This is a simple example for explaning late binding, Here we have used dynamic keyword which automatically detects opp data and depending on assigned data it get converts to that particular data-type, Here for example we have assigned a Int type data to dynamic object and to check its type on runtime behaviour we have used GetType().

As you data-type is not detected on compile time but it has detected on runtime and dynamic object gets converted into strong.Int32 type. Let's change the data and check the other outputs

class Program
    {
        static void Main(string[] args)
        {
            dynamic DynObj = 4.444;
            Console.WriteLine(DynObj.GetType());

        }
    }
    
 

class Program
    {
        static void Main(string[] args)
        {
            dynamic DynObj = "Quest";
            Console.WriteLine(DynObj.GetType());

        }
    }
    
 

Hence friends, It is been concluded that dynamic objects are detected during runtime and therefor it is late binding.

Friends in our coming article we are going to cover detailed article on reflection and dynamic keyword. So if you guys like this article feel free share it on facebook, twitter and google +.

+91-22-66752917
+91 9967590707
questpond@questpond.com / questpond@yahoo.com