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

M.F.M Fazrin
2 min readDec 16, 2022

--

In C#, late binding and early binding describe how the compiler handles calls to methods and properties at compile time.

Early binding, also called static binding or compile-time binding, happens when the compiler can figure out the type of an object at compile time and generate code to call the right method or property on that object. This allows the compiler to check for errors, such as typos or invalid method arguments, at compile time, rather than at runtime.

Late binding, also known as dynamic binding or runtime binding, occurs when the compiler cannot determine the type of an object at compile time, and generates code to perform the binding at runtime. This is typically done using the dynamic type or reflection. Late binding allows you to call methods and properties on objects that may not be known at compile time, but it can also make your code less efficient, since the binding must be done at runtime.

Here is an example of early binding in C#:

string s = "Hello, world!";
int length = s.Length; // Early binding

In this example, the compiler knows that the type of s is string, so it generates code to call the Length property on the string object.

Here is an example of late binding in C#:

dynamic d = "Hello, world!";
int length = d.Length; // Late binding

In this example, the compiler does not know the type of d at compile time, so it generates code to perform the binding at runtime. The actual type of d is string, so the Length property of the string object will be called.

I hope this helps! Let me know if you have any more questions about late binding and early binding in C# in the comment section.

--

--

M.F.M Fazrin
M.F.M Fazrin

Written by M.F.M Fazrin

Senior Software Development Specialist @ Primary Health Care Corporation (Qatar)

No responses yet