Click here to Skip to main content
Click here to Skip to main content
Go to top

Duck Typing with the Dynamic Keyword

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

Introduction

Disclaimer:

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)
{
  bird.Talk();
}

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

Quack
Honk

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)
{
  d.Talk();
}

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

Dyno-Quack!
Dyno-Honk!

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

static void Speak(dynamic d, string outWithIt)
{
  d.Talk(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! 

License

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

Share

About the Author

Marc Clifton

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

Comments and Discussions

 
GeneralMy vote of 5 PinmemberMihai MOGA13-Jun-13 20:39 
QuestionMy Vote of 5 PinmentorKeith Barrow3-Jun-13 0:15 
AnswerRe: My Vote of 5 PinprotectorMarc Clifton3-Jun-13 2:01 
GeneralRe: My Vote of 5 PinmentorKeith Barrow3-Jun-13 2:16 
GeneralMy vote of 5 PinmemberAmitosh Swain1-Jun-13 2:45 
GeneralMy vote of 4 PinprofessionalMohammed Hameed1-Jun-13 1:25 
GeneralMy vote of 5 PinmemberLeukocytes30-May-13 1:57 
GeneralMy vote of 5 Pinmemberdbrenth29-May-13 7:52 
Questionamusing and useful PinmemberCIDev29-May-13 4:13 
GeneralMy vote of 5 PinmemberRick-Z28-May-13 4:01 
GeneralMy vote of 5 Pinmemberhawk6627-May-13 20:38 
GeneralMy vote of 5 PinmemberScott Burkow27-May-13 5:34 
GeneralMy vote of 5 PinmemberKChandos25-May-13 5:59 
QuestionHilarious-knowelege Example! 5-o-5 Pinmemberprathameshpitale24-May-13 18:46 
AnswerRe: Hilarious-knowelege Example! 5-o-5 Pinmemberprathameshpitale24-May-13 18:49 
GeneralRe: Hilarious-knowelege Example! 5-o-5 PinprotectorMarc Clifton25-May-13 1:53 
GeneralOnly a quack would think duck typing is a good idea PinprofessionalPIEBALDconsult24-May-13 11:55 
GeneralRe: Only a quack would think duck typing is a good idea PinprotectorMarc Clifton24-May-13 12:02 
GeneralRe: Only a quack would think duck typing is a good idea PinprofessionalPIEBALDconsult24-May-13 13:34 
GeneralMy vote of 5 PinmemberBadassAlien24-May-13 10:40 
GeneralRe: My vote of 5 PinprotectorMarc Clifton24-May-13 11:11 
GeneralRe: My vote of 5 PinmemberSteve 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 | Mobile
Web03 | 2.8.140921.1 | Last Updated 24 May 2013
Article Copyright 2013 by Marc Clifton
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid