Dynamic Language Runtime
Explore how the Dynamic Language Runtime (DLR) allows C# to support dynamic typing and binding, enabling runtime discovery of object members, flexible property and method usage, and processing of unpredictable data like JSON. Understand the benefits and trade-offs of using dynamic features versus static typing in application development.
We'll cover the following...
Although C# is primarily a statically-typed language, it supports dynamic features through the dynamic language runtime (DLR). To understand this feature, we should consider the difference between statically and dynamically-typed languages. In languages with static typing, the identification of all types and their members (such as properties and methods) occurs at the compilation stage.
Consider the following Course class:
We have access to the Title property when we create an instance of the class. If we try to access a member that doesn’t exist, we get a compile-time error.
Next, we write a main program that attempts to access a non-existent property on this statically-typed object:
Line 3: We create an instance of the class using the
varkeyword and set theTitleproperty.Line 7: We attempt to print the
Durationproperty to the console, which triggers a compile-time error because the compiler verifies members beforehand.
In dynamically-typed languages, the runtime doesn’t know anything about the properties and methods of types until execution. With the help of the DLR, it’s ...