What is the purpose of the "dynamic" keyword in C#, and why would I want, or not want, to use it?
-
Do you mean main _purpose_? It is probably to ease COM interop (as are other features, like named and optional arguments), but I don't have any normative source for that, although I remember reading about it. I'm sure Dr. Google can help though. – Christian.K Jun 15 '12 at 06:28
-
very interesting.why two down votes?what is wrong with my question? – Behnam Esmaili Jun 15 '12 at 06:32
-
@BehnamEsmaili - Not my downvote but most likely because there are already several posts about this. – Lieven Keersmaekers Jun 15 '12 at 06:36
-
1@BehnamEsmaili probably that you should probably have RTFM *first*, then asked a question about what was unclear. Googling "dynamic keyword c#" yields, in the first 4, 2 MSDN articles and a blog by Scott Hanselman; glancing at those [this looks ideal](http://msdn.microsoft.com/en-us/library/dd264741.aspx). – Marc Gravell Jun 15 '12 at 06:37
-
@Lieven thanks for your response.but i am not agree with down vote because of duplicate question.i saw this sentence many times here "possible duplicate of ...".thanks anyway. – Behnam Esmaili Jun 15 '12 at 06:40
-
@MarcGravell what about learning good new stuff other than Scott's one? – Behnam Esmaili Jun 15 '12 at 06:42
-
@BehnamEsmaili You asked about the purpose etc; that hasn't suddenly changed since the documentation was written... – Marc Gravell Jun 15 '12 at 06:59
-
duplicates: http://stackoverflow.com/questions/2674906/when-should-one-use-dynamic-keyword-in-c-sharp-4-0 http://stackoverflow.com/questions/253460/what-is-the-practical-use-of-dynamic-variable-in-c-sharp-4-0?rq=1 http://stackoverflow.com/questions/689240/how-is-the-upcoming-dynamic-keyword-in-net-4-0-going-to-make-my-life-better?rq=1 http://stackoverflow.com/questions/2690623/what-is-the-dynamic-type-in-c-sharp-4-0-used-for?rq=1 – nawfal Jun 15 '12 at 07:01
2 Answers
In the statically typed world, dynamic gives developers a lot of rope to hang themselves with. When dealing with objects whose types can be known at compile time, you should avoid the dynamic keyword at all costs. When statically typing, dynamic doesn't make a stitch of sense. If you are dealing with an unknown or dynamic type, it is often necessary to communicate with it through Reflection. Reflective code is not easy to read, and has all the pitfalls of the dynamic type above. In this context, dynamic makes a lot of sense.
Underneath the hood, the dynamic type compiles down to wrapped reflective code, unless the object is a COM object (in which case, operations are called dynamically through IDispatch) or the object implements IDynamicObject, which is used extensively by IronPython and IronRuby and also in up-an-coming APIs.
More at MSDN

- 24,713
- 30
- 122
- 169
It was introduced to allow easier interop with dynamic languages.
Personally, I would not recommend to use it, unless you are going to interface with code written dynamically, or if you have a very specific reason to do.

- 11,087
- 8
- 47
- 61
-
thanks for your response too.may be Jaguar's answer is more complete. – Behnam Esmaili Jun 15 '12 at 06:52