When should you use a dynamic keyword in C # 4.0?

When do you need to use a dynamic keyword in C # 4.0? ....... A good example with a dynamic keyword in C # 4.0 that explains its use ....

+44
c # dynamic keyword
Apr 20 '10 at 12:10
source share
6 answers

Dynamic should only be used when its use is not painful . Like in MS Office libraries. In all other cases, it should be avoided, since compilation type checking is beneficial. The following is a good situation using dynamic.

  • Call javascript method from Silverlight.
  • COM interoperability.
  • Possibly reading Xml, Json without creating custom classes.
+29
Apr 20 '10 at 12:50
source share

How about this? I was looking for something and wondered why it is so difficult to do without the "dynamic" one.

interface ISomeData {} class SomeActualData : ISomeData {} class SomeOtherData : ISomeData {} interface ISomeInterface { void DoSomething(ISomeData data); } class SomeImplementation : ISomeInterface { public void DoSomething(ISomeData data) { dynamic specificData = data; HandleThis( specificData ); } private void HandleThis(SomeActualData data) { /* ... */ } private void HandleThis(SomeOtherData data) { /* ... */ } } 

You just need to catch the Runtime exception and handle it as you want, unless you have an overloaded method that accepts a particular type.

+8
Jan 14 '15 at 17:11
source share

As described here , dynamics can simplify the use of underdeveloped external libraries: Microsoft provides an example of Microsoft.Office.Interop.Excel. And with dynamics, you can avoid a lot of annoying explicit actuation when using this assembly.

In addition, unlike @ user2415376, this is definitely not a way to handle interfaces, since we already have Polymorphism implemented from the first days of the language!
you can use

  ISomeData specificData = data; 

instead

 dynamic specificData = data; 

In addition, it will make sure that you are not passing the wrong type of data object.

+2
Jan 23 '17 at 9:10
source share

Check out this blog post that talks about dynamic keywords in C #: https://cloudncode.blog/2016/07/12/c-dynamic-keyword-what-why-when/

Here's the bottom line: A dynamic keyword is really powerful, it is indispensable when used with dynamic languages, but it can also be used for complex situations when developing code where a statically typed object simply will not do.

Consider the disadvantages: 1. There is no compilation type check, which means that if you do not have 100% confidence in your unit tests (cough), you run the risk.

  1. A dynamic keyword uses more processor cycles than your old-fashioned statically typed code due to the extra overhead at runtime, if performance is important to your project (usually this), it doesn't use dynamic.

  2. Common errors include returning anonymous types wrapped in the dynamic keyword in public methods. Anonymous types are assembly specific, returning them through the assembly (via public methods) will throw an error, although simple testing will catch it, now you have a public method that can only be used from certain places, and this is just a bad design.

  3. The slope is too steep, inexperienced developers are eager to write something new and try to avoid more classes (this is not necessarily limited to inexperienced) will start to use the dynamics more and more if they see it in the code, usually I would do a code analysis check for dynamic / added it to the code review.

+1
Jan 02 '17 at 13:29
source share

It is definitely a bad idea to use dynamics in all cases where it can be used. This is because your programs will lose the benefits of checking compile time, and they will also be much slower.

0
Feb 22 '17 at 6:07
source share

I would like to copy an excerpt from a code project post that determines that:

Why use dynamic?

In a statically typed world, dynamics give developers a lot of rope to hang themselves. When working with objects whose types may be known at compile time, you should avoid the dynamic overhead keyword. Earlier, I said that my initial reaction was negative, so changed my mind? According to Margret Atwood, context is everything. when static typing, dynamics does not make any sense. If you are dealing with an unknown or dynamic type, you often need to communicate with it through Reflection. Reflective code is not easy to read, and has all the dynamic type traps above. In this context, dynamics make a lot of sense. [Details]

While some of the characteristics of the Dynamic keyword are:

  • Dynamically typed . This means that the type of the declared variable is decided by the compiler at runtime.
  • No need to initialize during declaration.

eg.

 dynamic str; str="I am a string"; //Works fine and compiles str=2; //Works fine and compiles 
  1. Errors fall at runtime

  2. Intellisense is not available because the type and its associated methods and properties can only be known at run time. [ https://www.codeproject.com/Tips/460614/Difference-between-var-and-dynamic-in-Csharp]

0
Feb 22 '17 at 6:27
source share



All Articles