DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Role of C# in Building Dynamic and Secure Web Applications
  • How to LINQ Between Java and SQL With JPAStreamer
  • Unlocking the Potential of Binary Search Trees with C# Programming
  • C# vs. Python vs. C++ for Game Development

Trending

  • Getting Started With Agentic Workflows in Java and Quarkus
  • Chaos Engineering Has a Blind Spot. Agentic AI Lives in It.
  • Every Cache Miss Is a Tiny Tax on Your Performance
  • Pragmatica Aether: Let Java Be Java

Dynamic C# Introduction

Dynamic C# offers dynamic capabilities, which are useful in certain situations. In this post, I will give you a very basic introduction to dynamic C#.

By 
Jawad Hasan Shani user avatar
Jawad Hasan Shani
DZone Core CORE ·
Feb. 05, 21 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
4.8K Views

Join the DZone community and get the full member experience.

Join For Free

Introduction

C# is generally considered as a statically typed language. However, Dynamic C# offers dynamic capabilities, which are useful in certain situations. In this post, I will give you a very basic introduction to dynamic C#.

Why Dynamic C#

There are many reasons why you would like to use dynamic C# capabilities. A 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 the amount of code
  • Useful when working with weakly typed data: JSON, XML, Plain Text etc
  • Interop with dynamic languages or writing COM interop code is easy in dynamic C#

It's like saying to the compiler, “Trust me, you will be fine during run time”

Example Usage

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

Static & Dynamic Binding

Let's see an example:

Static and Dynamic Binding Example

We can see that IDE is giving feedback for the Stop method not being available and the code will not compile due to this. This is the static binding behavior of C#.

Static Binding

  • Binding occurs at compile-time
  • The compiler knows that Stop() doesn’t exist in Printer
  • You receive a compile error, so it cannot build & execute the program

Dynamic Binding

Now, let's see the dynamic binding difference:

Dynamic Binding Example

  • Binding occurs at run-time
  • The compiler doesn’t know if the 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 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:

Example of Statically Typed Code

Now, see the use of ExpandoObject in the following code:


ExpandoObject Used in 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:

Dynamically Adding ExpandoObject Behavior

Summary

This was a short introduction of dynamic C# and we barely scratched the surface. 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. If you have some comments or questions, feel free to ask. You can see the code on this github repo. 

Until next time! Happy coding!

csharp

Published at DZone with permission of Jawad Hasan Shani. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Role of C# in Building Dynamic and Secure Web Applications
  • How to LINQ Between Java and SQL With JPAStreamer
  • Unlocking the Potential of Binary Search Trees with C# Programming
  • C# vs. Python vs. C++ for Game Development

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook