Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
Hello , I just understood that dynamic is unlike the object and can call methods and variables from the instance and not matter which type it is .
I want to know if i can use the dynamic keyword as usual instead of typing the type's name every time which also limiting the pointer for only one type of instances .
Secondly , i want to know much it affects the performance while using a lot of dynamic pointers in my code .
Third question , can i suddenly change the pointed instance of the dynamic pointer to another instance (for example from another dynamic pointer) and it will be quite fast and effective ?
Here some code :
dynamic Instance;
Instance = new Button();
Instance = new Panel();
Instance = 10;
About the 3rd question :
dynamic A = 10;
dynamic B = "string";
B = A;
Console.WriteLine(A + B); // Expected output : 20
Generally i want to know how much using "dynamic" is hurts the performance of my code . Because i am working on really critical class . But i also need the dynamic pointers (if not then the object pointer which are more problematic)
Posted 29-Dec-12 20:15pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

1) First of all, try it!
2) As I see, you mix/confuse var with dynamic. And are speaking pointers. But there are not pointers in c#. Actually there are, but not how you think (see:[^]). So if you really want to use this C/C++ approach, go on, but it will be hard.
3) What do you expect from changing the variable type? You gain nothing at all.
4) Behind the dynamic metatype there is a logic, there is lot of code. That code will take time to execute. This is why you will loose considerable performance when using it this way.
5) Dynamic is more useful in interop situations: Get rid of COM Interop DLL by using the new C# 4 dynamic keyword[^]
.. and more: Adventures with C# 4.0 dynamic - ExpandoObject, ElasticObject, and a Twitter client in 10 minutes[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 324
1 Sergey Alexandrovich Kryukov 182
2 Maciej Los 159
3 Hard_Rockz 143
4 Richard MacCutchan 125
0 OriginalGriff 5,374
1 Sergey Alexandrovich Kryukov 4,713
2 Peter Leow 2,944
3 DamithSL 2,465
4 Maciej Los 2,270

Advertise | Privacy | Mobile
Web02 | 2.8.140718.1 | Last Updated 30 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid