Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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 :
C#
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 :
C#
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

1 solution

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[^]
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900