Click here to Skip to main content
11,802,205 members (39,944 online)
Click here to Skip to main content

Duck Typing with the Dynamic Keyword

, 24 May 2013 CPOL 19.7K 33
Rate this:
Please Sign up or sign in to vote.
Don't do this at home.



Kids, don't do try at home.  Yes, we're using keywords and classes you can easily find in the cupboards of your .NET language, but please do not mix these keywords as described here.  Potentially lethal (to your job and someone else's sanity) will result.  The programming you are about to witness has been done by a professional, stunted developer. 

Now, let's go blow up some of your dull ideas about programming!

One of the interesting things about languages like Ruby is Duck Typing, the "if it walks like a duck, if it talks like a duck, if you're yard is covered in duck s***, why then, it must be a duck!"  Of course, with strongly typed languages like C#, we just couldn't do some of the abusive amazing things you will see in weakly typed languages.  Instead, we've had to rely on object-oriented concepts like inheritance, polymorphism, and interfaces to achieve some small degree of coolness. 

But no more!  The world has become the playground of all things that go quack in the night with the advent of the "dynamic" keyword!

The Shackles Of Strong Typing...

Oh, the boring things we have to do for the sake of strong typing:

public interface IQuack
  void Talk();

public class Duck : IQuack
  public void Talk() {Console.WriteLine("Quack");}

public class Goose : IQuack
  public void Talk() { Console.WriteLine("Honk"); }

How They Bind Us, Constrain Us...

static void Speak(IQuack bird)

IQuack bird1 = new Duck();
IQuack bird2 = new Goose();


But Free Us From The Shackles of Your Interfaces...

public class DynoDuck
  public void Talk() { Console.WriteLine("Dyno-Quack!"); }

public class DynoGoose
  public void Talk() { Console.WriteLine("Dyno-Honk!"); }

And We Become Free Men (and Women, and Birds)! 

static void Speak(dynamic d)

dynamic dynoBird1 = new DynoDuck();
dynamic dynoBird2 = new DynoGoose();


And Free of All Constraints Of Race, Creed, or...Class!

static void Speak(dynamic d, string outWithIt)

static dynamic InMyImage(string method, Action<string> action)
  dynamic dyn = new ExpandoObject();
  ((IDictionary<String, Object>)dyn)[method] = action;
  return dyn;

To Soar with the Gods!

var actor = (Action<string>) ((mySpeech) => Console.WriteLine(mySpeech));

dynamic dyn1 = InMyImage("Talk", actor);
dynamic dyn2 = InMyImage("Talk", actor);

Speak(dyn1, "Rise, my fellow ducks!");
Speak(dyn2, "To Canada or bust!"); 

Rise, my fellow ducks!
To Canada or bust! 

And Like Icarus, To Crash Back To Earth

As we realize freedom comes with a price: complete coding chaos! 


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Marc Clifton
United States United States
Marc is the creator of two open source projects, MyXaml, a declarative (XML) instantiation engine and the Advanced Unit Testing framework, and Interacx, a commercial n-tier RAD application suite.  Visit his website,, where you will find many of his articles and his blog.

Marc lives in Philmont, NY.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Duncan Edwards Jones29-Jul-15 20:41
professionalDuncan Edwards Jones29-Jul-15 20:41 
GeneralMy vote of 1 Pin
Craigoose3-Jun-15 2:42
memberCraigoose3-Jun-15 2:42 
GeneralMy vote of 5 Pin
Mihai MOGA13-Jun-13 20:39
memberMihai MOGA13-Jun-13 20:39 
QuestionMy Vote of 5 Pin
Keith Barrow3-Jun-13 0:15
mentorKeith Barrow3-Jun-13 0:15 
AnswerRe: My Vote of 5 Pin
Marc Clifton3-Jun-13 2:01
protectorMarc Clifton3-Jun-13 2:01 
GeneralRe: My Vote of 5 Pin
Keith Barrow3-Jun-13 2:16
mentorKeith Barrow3-Jun-13 2:16 
GeneralMy vote of 5 Pin
Amitosh Swain1-Jun-13 2:45
memberAmitosh Swain1-Jun-13 2:45 
GeneralMy vote of 4 Pin
Mohammed Hameed1-Jun-13 1:25
professionalMohammed Hameed1-Jun-13 1:25 
GeneralMy vote of 5 Pin
Leukocytes30-May-13 1:57
memberLeukocytes30-May-13 1:57 
GeneralMy vote of 5 Pin
dbrenth29-May-13 7:52
memberdbrenth29-May-13 7:52 
Questionamusing and useful Pin
CIDev29-May-13 4:13
memberCIDev29-May-13 4:13 
GeneralMy vote of 5 Pin
Rick-Z28-May-13 4:01
memberRick-Z28-May-13 4:01 
GeneralMy vote of 5 Pin
hawk6627-May-13 20:38
memberhawk6627-May-13 20:38 
GeneralMy vote of 5 Pin
Scott Burkow27-May-13 5:34
memberScott Burkow27-May-13 5:34 
GeneralMy vote of 5 Pin
KChandos25-May-13 5:59
memberKChandos25-May-13 5:59 
QuestionHilarious-knowelege Example! 5-o-5 Pin
prathameshpitale24-May-13 18:46
memberprathameshpitale24-May-13 18:46 
AnswerRe: Hilarious-knowelege Example! 5-o-5 Pin
prathameshpitale24-May-13 18:49
memberprathameshpitale24-May-13 18:49 
GeneralRe: Hilarious-knowelege Example! 5-o-5 Pin
Marc Clifton25-May-13 1:53
protectorMarc Clifton25-May-13 1:53 
GeneralOnly a quack would think duck typing is a good idea Pin
PIEBALDconsult24-May-13 11:55
professionalPIEBALDconsult24-May-13 11:55 
GeneralRe: Only a quack would think duck typing is a good idea Pin
Marc Clifton24-May-13 12:02
protectorMarc Clifton24-May-13 12:02 
GeneralRe: Only a quack would think duck typing is a good idea Pin
PIEBALDconsult24-May-13 13:34
professionalPIEBALDconsult24-May-13 13:34 
GeneralMy vote of 5 Pin
BadassAlien24-May-13 10:40
memberBadassAlien24-May-13 10:40 
GeneralRe: My vote of 5 Pin
Marc Clifton24-May-13 11:11
protectorMarc Clifton24-May-13 11:11 
GeneralRe: My vote of 5 Pin
Steve Wellens27-May-13 7:57
memberSteve Wellens27-May-13 7:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151002.1 | Last Updated 24 May 2013
Article Copyright 2013 by Marc Clifton
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid