Introduction
When an object is assigned to a specified kind of object variable, the C# compiler uses the.NET Framework to perform the binding. C# can perform two different types of bindings:
- Early Binding
- Late Binding
Let us discuss these two types of bindings in detail to understand them better.
Early Binding
Early binding refers to detecting and verifying methods and properties during the compilation process. The compiler is aware of the kind of object in this case. The compiler throws an exception during compile time if a method or property does not exist or has a data type problem. Early binding improves performance and makes development easier. Because there is no typecast in early binding, the application runs faster. Early binding minimizes the number of run-time errors.
Example:
Here is an example based on Early binding.
using System;
class Ninjas{
public string name;
public string language;
public void Set_value(string name, string language)
{
this.name=name;
this.language=language;
Console.WriteLine("My name is: "+name);
Console.WriteLine("My Favorite Programming language is: "+language);
}
}
class Program
{
static void Main(string[] args)
{
Ninjas obj=new Ninjas();
obj.Set_value("Alok", "C#");
obj.Display();
}
}
Output
prog.cs(19,13): error CS1061: Type `Ninjas' does not contain a definition for `Display' and no extension method `Display' of type `Ninjas' could be found. Are you missing an assembly reference?
prog.cs(2,7): (Location of the symbol related to previous error)
Compilation failed: 1 error(s), 0 warnings
Here from the above code, we can see that there is no "Display" method written in the class code. Hence the compiler throws an error at compilation time.