bash74
bash74

Reputation: 767

C# dynamic types - heaven or hell?

Do you think the new dynamic type for C# is a serious drawback regarding the advantages of a strong typed language or do you think the advantages (like getting around those heavily reflections) outweigh the risks?

Upvotes: 5

Views: 626

Answers (3)

TrueWill
TrueWill

Reputation: 25563

If used sparingly and with care, I find it to be a valuable addition. As @JWL_ mentioned, doing COM late binding in C# was painful prior to dynamic.

Run-time overload resolution is another area where dynamic shines. When you want to process descendants of a base type polymorphically and you don't control the hierarchy, this eliminates the need for a big switch statement with casting. Note that you can trap the specific run-time exception.

I've run into this when implementing TrackingParticipant.Track in WF4. Your code needs to know the run-time type of the TrackingRecord (see the partial hierarchy).

Upvotes: 0

Chris S
Chris S

Reputation: 65456

The main use of the dynamic keyword appears to be for ASP.NET interaction where you are working with a weakly typed language like Javascript, or for passing parameters inside ASP.NET MVC helper classes in a view.

I don't think any experienced C# programmer will start to use it for a replacement of the type system, and if they did they'd be shot down fairly quickly.

Upvotes: 6

JWL_
JWL_

Reputation: 829

As long it isn't used in the wrong places I think it's a great addition to the language and framework. To use dynamic just because your lazy is a bad thing but when you really need it (COM, interaction with javascript and what have you etc) it's great!

Upvotes: 6

Related Questions