Click here to Skip to main content
Rate this: bad
good
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();
Console.WriteLine(Instance.Text);
Instance = new Panel();
Console.WriteLine(Instance.Padding);
Instance = 10;
Console.WriteLine(Instance);
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
good
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: http://www.c-sharpcorner.com/UploadFile/rajeshvs/PointersInCSharp11112005051624AM/PointersInCSharp.aspx[^]). 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[^]
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Guruprasad.K.Basavaraju 490
1 Sergey Alexandrovich Kryukov 316
2 Shai Vashdi 314
3 OriginalGriff 190
4 praveen_07 115
0 Sergey Alexandrovich Kryukov 9,185
1 OriginalGriff 5,315
2 Peter Leow 4,040
3 Maciej Los 3,535
4 Abhinav S 3,308


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