Dynamic in C# – Introduction

Posted: September 5, 2009 in .NET, C#
Tags: , ,

The “dynamic” is the new keyword introduced in C# in version 4.0.This adds some kind dynamic capabilities in this statically typed language.But before getting into the details of how this new keyword will be used and what are it’s implication I would prefer to take a quick look into what are dynamic languages, duck typing etc.

In the dynamic languages some of the common operations like type checking, method dispatch etc. are performed at runtime whereas in static languages these are performed at compile time.The dynamic languages are not something new, they are in the industry for quite sometime now.But with the advent of web some of these languages were proven to be extremely useful and the dynamic programming community has growing since then.The languages like PHP,Ruby,Python, Javascript, Groovy etc. are the popular and mainstream dynamic programming languages today.Let’s take a look at the following piece of Ruby code to get a feel of dynamic languages.


i = "Sankarsan"
j = "Bose"
puts i + j
# This will print as "SankarsanBose"
i = 50
j = 50
puts i + j
#This will print 100

So the type of the variables is defined dynamically at runtime based on the value it is holding.

Along with this another important thing to notice is how method dispatch is handled in dynamic languages.Let’s take a look into the following piece of code in Ruby.


class WordProcessor
  def print
    puts "Printing Document"
class Spreadsheet
  def print
    puts "Printing Spreadsheet"
class OfficeSuite
  def print(p)

w = WordProcessor.new();
s = Spreadsheet.new();
o = OfficeSuite.new();

Here the output will be:

Printing Spreadsheet

Printing Document

Here we are not bothered about what type “p” has, as long as it supports a method called print() we are good.This is what is known as “Duck Typing“.The essence of this is

“when I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.”

This means as long as the object has ability to execute a method it is fine I need not bother about the type (that is it is a duck or not)

Actually with the new “dynamic” keyword in C# we are going to have the ability of duck typing.

But before getting I would like explain briefly about DLR (Dynamic Language Runtime) in my next post.This will make it easy to understand later how “dynamic” is implemented.

PS: Some of the contents here are taken from earlier post in arch2dev.com

  1. […] 13, 2009 by sankarsan In my last post I have covered the basics of dynamic languages and now we will see how this dynamic languages fit […]

  2. Ecko says:

    nice post… thanks…

  3. Ecko says:

    nice article, thanks.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.