Dynamic C# Introduction

Introduction

C# is generally considered as a statically typed language. However it also offer very useful dynamic capabilities which comes handy in certain situations. In this post I will give you a very basic introduction of dynamic C#.

Why Dynamic C#

There are many reasons why you would like to use dynamic C# capabilities. Few of these are mentioned below:

  • It compliments “normal” statically typed C#.
  • We can use it when we don’t know object/data structure at compile time.
  • It can also improve source-code readability. It may also reduce amount of code.
  • Useful when working with weakly typed data: JSON, XML, Plain Text etc.
  • It can also help us with COM interop code.
  • Allow us to interop with dynamic languages.

Its like saying to compiler:

“I know you don’t know if you can do this or not, but just trust me, I know at runtime that everything will be fine”

Example Usage

  • Replace reflection code with dynamic code.
  • Simple COM interop.
  • Dynamic JSON processing.
  • Data Access Code

Static & Dynamic Binding

Here I have some code:

We can see that IDE is giving feedback for Stop method not being available and code will not compile due to this.

Static Binding:

  • Binding occurs at compile-time.
  • Compiler knows that Stop() doesn’t exist in Printer.
  • Compile error, cannot build & execute program

Dynamic Binding:

Now, see the dynamic binding difference:

  • Binding occurs at run-time.
  • Compiler doesn’t know if Stop() method exists or not.
  • Program compiles and can be executed (runtime error may result).
  • Even with dynamic C#. type safety is still enforced, only this time it’s at run-time.

Limitations of Callable Methods

We can’t use extension-methods with dynamic types. Extension methods are compile time concepts. However we can still utilize extension methods by calling them as normal static methods.

Introducing ExpandoObject

It is a pre-supplied dynamic type. It is a general purpose class. We can think of ExpandoObject as being similar to a dictionary that has string-based keys and object values.

To understand ExpandoObject, lets first see a typical statically typed code:

Now, see the use of ExpandoObject in following code:

Dynamically Adding ExpandoObject Behavior

In addition to dynamic properties, we can also add behavior. Any object can be added as a value even behavior.

Summary

This was a short introduction of dynamic C#. We learned that it complements static C# and comes in handy in certain situations. Correctly applied, dynamic C# has the ability to improve productivity, readability & simplicity of code. We also learned about ExpandoObject and saw that we can dynamically add properties and behavior using ExpandoObject. In next post on this topic we see more examples of dynamic C# uses. If you have some comments or questions, feel free to ask. You can see the code on this github repo. Till Next Time, Happy Coding.

My Recent Books

1 thought on “Dynamic C# Introduction”

Comments are closed.