6

I just read a chapter in a programming book about Dynamic types. Although they are quite neat I cant think of a single real world example where I would use them. Does anyone here actually use them and for what?

1
  • See my question f.e.: stackoverflow.com/questions/6883741/… - the difference between Jon's solutions for 4.0 and 3.5. What I needed it for was feeding business objects with data from parsed CSV, whose format I wouldn't know beforehand. It could be [id][name][category][id][category][name] or whatever, so I'd just recognize the type of the busines object that was needed and then used a generic class for assembling them, making sure that the 1st occurence of attribute X goes to the 1st instance of class Y, the 2nd - to the 2nd etc. Aug 2, 2011 at 8:37

3 Answers 3

5

I've used dynamic to evaluate logical expressions represented in an object graph, where using the dynamic keyword allowed me to largely ignore the types of the operands.

For example, if my expression graph was an arithmetic expression with a multiplication operator (e.g. 12 * 1.3), I would use dynamic to store the operands 12 and 1.3, and dynamic again to catch the result from the multiplication.

Without dynamic I would be forced to deal with concrete numeric types (int/long/decimal/float, etc...) and the resulting code bloat. My dynamic solution requires very little code and is much easier to grasp and maintain.

My initial idea was to turn my logical expressions into snippets of Python or Ruby, then run these snippets through IronPython or IronRuby to evaluate the result. It struck me all I required from Python or Ruby was their dynamic typing, so I was very happy to see that dynamic affords Python-like typing for C#.

6
  • Numeric calculation is a great idea. Aug 2, 2011 at 11:30
  • This sounds like a good idea, but I'm having trouble getting my head around it. How was dynamic better than using decimal for everything?
    – pdr
    Aug 2, 2011 at 13:34
  • @pdr: using dynamic avoids all casting and fewer casts make source code cleaner and easier to maintain.
    – Ed James
    Aug 2, 2011 at 13:59
  • @Ed: I mean, why are they all different types to start with? Where are they coming from? Is there not a risk here of dividing a decimal by a double and ending up with inaccuracies you didn't expect?
    – pdr
    Aug 2, 2011 at 14:03
  • @pdr: the real answer to your questions requires more space and time than I can provide right now. The quick answer is that most of the expression are user input, so it's the problem of user if expression produces inaccurate results or even fails to evaluate.
    – Ed James
    Aug 3, 2011 at 9:49
1

In addition to previous answers relating to dynamic languages, dynamic typing in C# also eases a lot of pain when doing COM interop - e.g. see this MSDN article , or scroll down to the bottom of this post on Chris Burrows' blog.

-1

I cant think of a single real world example where I would use them

It's a framework, it isn't always about you. In the case of dynamic alot of the backing came from the need to support more dynamic languages in the CLR, such as F# or IronPython. The fact that it helps cut down on code in other ways is an added bonus.

3
  • 4
    What do you mean, 'it isn't about you'?
    – JeffO
    Aug 2, 2011 at 11:47
  • added the quote to help it make sense. Aug 2, 2011 at 12:03
  • 5
    @Wyatt Barnett even if I dont use them I would still like to know what they are used for Aug 2, 2011 at 12:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.