Late binding in C# refers to the process of resolving method calls, property accesses, or object type references at runtime, rather than at compile time (which is known as early binding). Late binding allows for more flexibility in the code because it can work with objects and methods that are determined dynamically during the execution of the program, making it useful in scenarios like working with COM objects, reflection, or interacting with types that are unknown at compile time.
Runtime Resolution: Late binding occurs when the exact method or property to be invoked is not determined until the program is actually running.
Dynamic Types: In C#, the dynamic keyword enables late binding, allowing you to work with objects whose type is not known at compile time.
Reflection: Reflection in C# provides a way to inspect and interact with assemblies, types, and members (methods, properties, fields) at runtime. This is a form of late binding because it allows invoking members dynamically based on their names or types at runtime.
Early Binding:
var person = new Person();
person.PrintName(); // Early binding
Late Binding:
dynamic keyword or reflection to call methods or access properties.dynamic person = new Person();
person.PrintName(); // Late binding
dynamicIn C#, the dynamic keyword is used to enable late binding. When you declare an object as dynamic, the type is resolved at runtime, and the compiler doesn't check for method existence or type correctness at compile time. This allows you to invoke methods, properties, or access fields dynamically.
dynamicusing System;
public class Person
{
public string Name { get; set; }
public void PrintName()
{
Console.WriteLine("Name: " + Name);
}
}
public class Program
{
public static void Main()
{
dynamic person = new Person();
person.Name = "John Doe"; // Setting property dynamically
person.PrintName(); // Calling method dynamically
// Using late binding to access methods or properties
dynamic unknown = "Hello, dynamic world!";
Console.WriteLine(unknown.ToUpper()); // Calls ToUpper method dynamically
}
}
person variable is declared as dynamic, so its type is determined at runtime.PrintName method is called dynamically, meaning the compiler does not verify that the method exists at compile time.PrintName does not exist on the Person object, it will throw a runtime error.Reflection provides a way to inspect and interact with types and their members at runtime. Using reflection, you can dynamically invoke methods, get or set properties, and even instantiate types that are not known at compile time.
using System;
using System.Reflection;
public class Person
{
public string Name { get; set; }
public void PrintName()
{
Console.WriteLine("Name: " + Name);
}
}
public class Program
{
public static void Main()
{
// Create an instance of Person using reflection
Type personType = typeof(Person);
object personInstance = Activator.CreateInstance(personType);
// Set the 'Name' property using reflection
PropertyInfo nameProperty = personType.GetProperty("Name");
nameProperty.SetValue(personInstance, "John Doe");
// Call the 'PrintName' method using reflection
MethodInfo printNameMethod = personType.GetMethod("PrintName");
printNameMethod.Invoke(personInstance, null); // Invoke without parameters
}
}
Person dynamically.Name property and the PrintName method.Reflection can be more powerful than dynamic in some scenarios, as it allows access to private members, more fine-grained control over method invocation, and the ability to examine assemblies.
While late binding offers significant flexibility, it comes at a performance cost:
It's generally best to use late binding only when necessary, such as when working with COM objects, interacting with dynamically loaded assemblies, or when the type of an object is unknown at compile time.
Interoperating with COM objects: When you work with COM components, their methods and properties are not known at compile time. Late binding is used to interact with COM objects dynamically.
Plug-in architectures: In systems where you load assemblies or types dynamically (such as a plugin system), late binding can be useful for invoking methods on objects that are unknown at compile time.
Serialization/Deserialization: When you deserialize data (like JSON or XML) into objects, the types may not be known at compile time. Late binding can be used to manipulate these objects once they're deserialized.
Dynamic Object Interaction: Sometimes, you may not know the structure of an object in advance, such as when working with objects received from external APIs or user input. The dynamic keyword allows you to interact with such objects without prior knowledge of their type.
Late binding in C# provides the ability to work with objects, methods, and properties whose types and members are determined at runtime. Using the dynamic keyword or reflection, you can invoke methods or access members dynamically, making your application more flexible. However, while this flexibility is useful, it comes with performance costs and potential runtime errors, so it should be used judiciously.
Open this section to load past papers