Home >Backend Development >C#.Net Tutorial >A brief discussion of early binding and late binding in C#
When an object is assigned to an object variable, the compiler performs a process called binding. C# performs two different types of binding: early binding and late binding. The following article will introduce you to these two bindings. I hope it will be helpful to you.
Early binding
Early binding is also called static binding, which refers to binding at compile time. Defined; early bound objects are basically strongly typed objects or statically typed objects. [Tutorial Recommendation: C# Video Tutorial]
Early binding identifies and checks methods, properties, and functions during compilation, and performs other optimizations before application execution. In this binding, the compiler already knows what type of object it is and what methods or properties it has.
Advantages of early binding
1. Fast performance
2. Easy to code and develop
3. Reduced Number of runtime errors
Example:
using System; class People { // data members public string name; public string subject; // public method public void details(string name, string subject) { this.name = name; this.subject = subject; Console.WriteLine("我的名字是: " + name); Console.WriteLine("我最喜欢的科目是: " + subject); } } class GFG { // Main 方法 static void Main(string[] args) { // 创建People类的对象 People g = new People(); // 调用People类的方法 g.details("Ankita", "C#"); // 调用“myMethod()”时出错 // 因为这个方法不属于People类,或者编译器在编译时不知道mymethod() g.mymethod(); } }
An error occurred while compiling:
Description :In the above example, we have a class called People. This class contains the details() method. Here, the compiler already knows the properties and methods that exist in People. But when we try to call mymethod(), it throws an error because the compiler doesn't know about this method.
Late binding
Late binding is also called dynamic binding. In late binding, the compiler does not know what type of object it is and what methods or properties it has, here the object is a dynamic object. The type of an object is determined based on the data it holds on the right side at runtime. Basically, late binding is achieved by using virtual methods.
The biggest advantage of late binding is that this type of object can hold a reference to any object, but it lacks many of the advantages of early binding objects. For example: late binding performs slower than early binding because it requires lookups at runtime.
Example:
using System; class GFG { static void Main() { // Dynamic objects dynamic obj = 4; dynamic obj1 = 5.678; // 显示对象的类型 Console.WriteLine("对象类型为:"); // 使用getType()方法来获取类型 Console.WriteLine(obj.GetType()); Console.WriteLine(obj1.GetType()); } }
Output:
Description:
In the above example, obj stores integer type data and obj1 stores double type data. But the compiler doesn't resolve these issues at compile time. At runtime, these dynamic objects are detected and converted to System.Int32 and System.Double respectively. This is why the runtime resolution process is called late binding.
The above is the entire content of this article, I hope it will be helpful to everyone's study. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !
The above is the detailed content of A brief discussion of early binding and late binding in C#. For more information, please follow other related articles on the PHP Chinese website!