What is difference between late binding and early binding in C#?

In C#, late binding and early binding are two different approaches for binding method calls to their implementations. The main difference between them lies in the timing and mechanism of resolving the method call at runtime.

  1. Early Binding:
    • Early binding, also known as static binding, occurs at compile-time.
    • The method call is resolved and bound to the specific implementation during the compilation phase.
    • The compiler knows the exact type of the object at compile-time, and the method call is directly linked to the appropriate method in the compiled code.
    • Early binding provides faster method resolution at runtime compared to late binding.
    • Early binding is the default behavior in C# when calling methods on objects with known types.

Example of early binding in C#:

class BaseClass
    public void Method()
        Console.WriteLine("Base class method");

class DerivedClass : BaseClass
    public new void Method()
        Console.WriteLine("Derived class method");

BaseClass obj = new DerivedClass();
obj.Method(); // Resolves to BaseClass.Method() at compile-time

In this example, even though the object is of type DerivedClass, the method call obj.Method() is resolved to the base class method BaseClass.Method() at compile-time due to early binding.

  1. Late Binding:
    • Late binding, also known as dynamic binding or runtime binding, occurs at runtime.
    • The method call is not resolved and bound to the specific implementation until runtime.
    • The compiler does not have knowledge of the exact type of the object at compile-time.
    • Late binding is typically used when the type of the object is determined dynamically, such as when working with reflection, COM interop, or dynamic typing.
    • Late binding provides flexibility in resolving method calls but may introduce performance overhead due to the dynamic resolution process.

Example of late binding in C# using reflection:

class MyClass
    public void Method()
        Console.WriteLine("MyClass method");

Type type = Type.GetType("MyNamespace.MyClass");
object obj = Activator.CreateInstance(type);
MethodInfo method = type.GetMethod("Method");
method.Invoke(obj, null); // Resolves to MyClass.Method() at runtime

In this example, the method call method.Invoke(obj, null) uses reflection to dynamically bind and invoke the method MyClass.Method() at runtime based on the provided type and method name.

In summary, the main difference between late binding and early binding in C# is the timing of method resolution. Early binding occurs at compile-time, providing faster method resolution, while late binding occurs at runtime, allowing for dynamic method resolution. Early binding is the default behavior when the object type is known at compile-time, while late binding is useful in scenarios where the type is determined dynamically or when working with reflection and dynamic typing.

error: Content is protected !!