Click here to Skip to main content
15,920,005 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
See more:
Hi everyone,

I have come across some material that talks a bit about different variables that are used in c# but i am not sure that i fully understand it yet. I am trying to understand these two specifically:

C#
dynamic obj;

and
C#
object obj;


What are the differences between the two?
What exactly do they do?
How and when is a dynamic value used?

Thanks for the help in advance. :)
Posted
Comments
Sergey Alexandrovich Kryukov 12-Oct-15 22:54pm    
What's wrong with reading original MSDN documentation?
—SA

Welcome to CodeProject !

Your question suggests to me that you need to take some to study the essentials, the basics, of .NET and C#. The links in the answer by Sergey above are very good resources for your study.

I suggest you download the excellent (free) book, ".NET Book Zero" by Charles Petzold: [^], and go through it carefully.

Object Type: Every Class in .NET inherits from the root structure/Type named 'object. What does that mean: it means there is a template for the allocation of memory ... a pointer and other information ... fundamental to the creation of every Class, fundamental to the creation of instances of any Class. Note: Value Types in .NET inherit from ValueType which inherits from Object.

Yes, there are other aspects of what the Object Type is, and how it "works," but those can wait until later. In the future you can study in depth: stack, heap, ValueType vs. ObjectType, boxing/un-boxing, etc.

Dynamic Type (C# >= 4): C# is a strongly typed language where the Compiler must "know" ... at compile time ... the Type of each field, each Class, each Property, each object a method may accept as a parameter, or return as a result.

It is also correct to say C# demands early-binding to compile.

But, there are situations where one would want late-binding, to defer the compiler's allocation of memory and behavior ... to handle the Type at run-time ... based on Type ... why ? ... because you may have objects that you wish to deal with of different Types, and those Types are not known to your program at compile time.

There is a "price" for using late-binding in C# by using the System.Dynamic namespace: the compiler at run-time must resolve the Type using its Name; the allocation of memory and construction of the required internal state with each "new" Type your dynamic structure, or field, has assigned to it. Method calls will be slower.

Good article on late-binding and System.Dynamic here by one of the people at MS who worked on implementing it in C#: [^].

Now: let's see you get busy, studying, coding, analyzing your code and its behaviors, learning how to debug.
 
Share this answer
 
Comments
Silver13 13-Oct-15 18:50pm    
I red on one of the articles that Dynamic is one of the underused feature of the code. That's what got me thinking about it.

Also, right here on Code Project i came across this.
http://www.codeproject.com/Tips/460614/Difference-between-var-and-dynamic-in-Csharp

Very informative answer to my question. Thanks Bill. I am definitely going to look at the free book you suggested.
Please see my comment to the question.

Please see:
https://msdn.microsoft.com/en-us/library/dd264736.aspx[^],
https://msdn.microsoft.com/en-us/library/dd264741.aspx[^],
https://msdn.microsoft.com/en-us/library/dd233052.aspx[^].

As you can see, there is a whole lot of stuff to understand. And if you want to know how other, non-dynamic types work, you will have to learn pretty much all the basics of .NET, which I would highly encourage to do.

The question about "difference" is totally incorrect, as well as most other "difference" questions. (If it's not quite obvious to you, please tell us the difference between apple and Apple :-)). And an answer to the question "what exactly they do" make take a whole big article, if not a book, but understanding of things may take something ranging from reading of the MSDN articles referenced above to learning major part of .NET, CLR anc CLI.

—SA
 
Share this answer
 
Comments
Silver13 13-Oct-15 18:36pm    
Thanks for the links, i have red them already. I do understand a bit about it but i cannot say that i truly understand it yet.

Just as you said it: "tell us the difference between apple and Apple"

I came to the same conclusion that they appear same but are not quite.. hence why i wanted someone with more experience help me understand it better.

Why do we have two things that are capable of seeming same things. Thanks for the second link though, i missed that one.
Sergey Alexandrovich Kryukov 13-Oct-15 19:29pm    
All right, thank you for the understanding.
They are not doing same things, even seemingly. If you want my opinion, I would stay away from dynamic, unless you want to support interop with dynamic languages. .NET programming is strongly types.
—SA

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