2

With dynamic we pretty much have a dynamic pointer, but not exactly a dynamic object. The true dynamic object in C# is the ExpandoObject, but that is a really unknown class for most of people. The expando allows creating and removing members at runtime, much like a hash (similar to JavaScript).

Why the ExpandoObject goodness was implemented in a separate class rather than just being, let's say, implemented as a feature of anonymous types?

Maybe that wouldn't be a good move because the lacking of type-safety? Or maybe due the (DLR) overhead involved?

talles
  • 14,356
  • 8
  • 45
  • 58
  • I use anonymous types probably a dozen times a day, but I've only had to use ExpandoObject and dynamic a handful of times in the past year. In other words, 99% of the time [YAGNI](http://en.wikipedia.org/wiki/You_aren't_gonna_need_it) – p.s.w.g Jan 13 '14 at 21:24
  • The type of anonymous types is known at compile time. They don't use the DLR. I'm not sure I see a connection? See http://stackoverflow.com/a/392163/453277 – Tim M. Jan 13 '14 at 21:24
  • 2
    "Most of the time that we use the dynamic in C# is when we deal with anonymous types." What? You shouldn't be using `dynamic` at all when dealing with anonymous types. If you are, you're *using that feature incorrectly*. Anonymous types are designed to create simple objects that will have *compile time type safety*, which you're *throwing right out the window when using `dynamic`. – Servy Jan 13 '14 at 21:25
  • 2
    Anonymous types were implemented long before `dynamic`. – Blorgbeard Jan 13 '14 at 21:25
  • you have a big misunderstanding. Please read up on anonymous types (compile-time resolved, strongly-typed, unnamed classes) versus dynamic behavior (run-time resolved, non-type-safe) – Federico Berasategui Jan 13 '14 at 21:27
  • @Servy most of the time I **have** to use dynamic I'm dealing with anonymous types. I doesn't mean that every time I'm dealing with anonymous I'm in use for dynamic. The statement only goes one way around, big difference. – talles Jan 13 '14 at 21:27
  • 1
    @talles You shouldn't be using `dynamic` with anonymous types at all. If you're using them together at all, odds are something is wrong. Your use of anonymous types shouldn't be with `dynamic`, your use of `dynamic` generally shouldn't involve anonymous types. – Servy Jan 13 '14 at 21:28
  • @Servy I'm `guessing` that the OP is using `dynamic` to pass instances of anonymous types between methods? such as `public void DoSomething(dynamic myanonymoustype)` which wouldn't otherwise be possible? – Federico Berasategui Jan 13 '14 at 21:29
  • 1
    @HighCore Yes, that is my guess as well. And of course, you shouldn't be doing that; it is contrary to the intended purpose of anonymous types. – Servy Jan 13 '14 at 21:29
  • @HighCore But it somehow break anonymous types suggested usage, which makes them useful rather as local variable. And if you really need to pass them to another method you should make the method generic and let compiler infer type for you. – MarcinJuraszek Jan 13 '14 at 21:30
  • 1
    @talles if the scope of any given `variable` exceeds the Method scope, and you have to use it in another methods or classes, then create a proper, strongly typed POCO object to hold the data which you're currently putting inside the anonymous type. – Federico Berasategui Jan 13 '14 at 21:30
  • This pretty much turned up into a chat. Not a really good question (closing). I got my answer in the end (type safety... but also I completely forgot the DLR as @TimMedora pointed out). – talles Jan 13 '14 at 21:33

1 Answers1

7

Because anonymous types have other very important feature - they provide you compile time type safety.

And because dynamic and anonymous types are just different concepts. The first one gives you ability to dispatch object members at runtime, the second lets you create statically typed objects with some base functionality (equality, hashcode, etc) without creating corresponding POCO classes. Why should they be implemented in the same way then?

btw. I use them quite a lot and really rarely needed to use dynamic to deal with them. Are you sure you're using these language features correctly?

Update

I think that's very important part of anonymous types tutorial:

If you must store query results or pass them outside the method boundary, consider using an ordinary named struct or class instead of an anonymous type.

MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
  • Why the assumption that I have to do this often? – talles Jan 13 '14 at 21:33
  • 1
    +1. @talles - I don't think this answers assumes anything about frequency of your usage (also your wording of that sentence in question can lead to such assumption, especially "mostly ... *we*..." part). I guess `dynamic` is just not most favorite feature of people coming from strongly typed languages - so you get unrelated chat about using it. – Alexei Levenkov Jan 13 '14 at 21:38
  • 1
    @AlexeiLevenkov I don't mind using `dynamic` in cases it was designed to be used in. Anonymous types are not good case for `dynamic`. – MarcinJuraszek Jan 13 '14 at 21:39
  • @AlexeiLevenkov, you are right, I re-read my question and it may sounded like that. I reworded it, but it's pretty much ruined now. Sometimes that avalanche that SO users does are harsh. – talles Jan 13 '14 at 21:45
  • 1
    I actually do mind using `dynamic` - it is so much more painful to debug, especially whit exceptions turned on to "when thrown" - almost every call to unknown property causes an exception... but unfortunately there are case where they are used in framework like `ViewBag` in ASP.Net MVC... – Alexei Levenkov Jan 13 '14 at 21:56
  • @talles thats for updating post, looks less controversial now. If you add sample where you find necessary to use `dynamic` it may make it even better (or extremely controversial again - be carfull :) ) – Alexei Levenkov Jan 13 '14 at 21:57
  • @AlexeiLevenkov Well, it was added to the language specifically for dealing with interoping with other dynamic languages in which all of those problems already existed. *That* is what it's supposed to be used for, and when used in that major it doesn't introduce (additional) problems. – Servy Jan 13 '14 at 22:07
  • @AlexeiLevenkov it has been useful for me in the past when I had to deal with some, let's say, *flexible* data. Right now (the expando thing) is been useful to a colleague that is developing a somewhat complex objects logging/comparison library. Yes, these are exceptional cases and I'm \*definitely\* not bringing it here. By the way, thank you for being kind and actually trying to help (: – talles Jan 14 '14 at 00:26