Interview question - dynamic vs object

I had an interview the other day and they let me keep the test. I'm reviewing it to learn more...

One of the questions is: difference between  declaring an argument as "dynamic" vs "object". I answered #2 below. I've been reading about it and I think it's correct because of boxing and unboxing --> dynamic doesn't need it but Object can be cast. Is this correct?

b1.png
LVL 8
CamilliaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kaufmed   ( ⚆ _ ⚆ )I asked the operating system what I could do to become a better programmer. It said, "Let me give you some pointers."Commented:
dynamic was really created to support interop with duck-typed languages, such as Python. Such languages don't evaluate the types of their variables until runtime, whereas C#'s types are evaluated at compile-time. People just use dynamic without interop-ing with duck-type languages because it's there--just like people (ab)use var because it's there.

You can certainly cast dynamic, but you risk the cast failing if you have an incorrect type in the dynamic.

e.g.

dynamic x = 1;
int y = (int)x;

Console.WriteLine(y);

Open in new window


dynamic has no Intellisense support--at least none that I've ever seen. object has full Intellisense support--object just doesn't have very many members.

You can certainly assign to a dynamic...you can even change the type of the thing that's in the variable. But again, if you do that, and you try cast later...

e.g.

dynamic x = 1;
int y = (int)x;

x = 3;
y = (int)x;

x = "duck";
y = (int)x;    // ruh-roh Raggy!  Scooby Dooby-Dooooooo

Console.WriteLine(y);

Open in new window


You do not have to cast a dynamic in order to execute any of its methods...you just don't get Intellisense on the dynamic, so if you do cast, then you will get Intellisense. Without casting, you have to know what the dynamic variable points to and what properties or methods that class offers.

e.g.

dynamic x = new List<int>();

x.Add(1);

Console.WriteLine(x[0]);

Open in new window


Remember, the big thing with dynamic is that everything happens at run-time. We call this "late binding" in programming. object is a regular old (static, as in "not dynamic" not the static keyword) type in C#. For this reason, we get compile-time support with it. This is why you have Intellisense on object.
CamilliaAuthor Commented:
Fantastic explanation. Thanks for taking the time to do this. Comparing what you have, the answer is the first one in the list I have above;
dynamic is not compile time and object doesn't have many members.
kaufmed   ( ⚆ _ ⚆ )I asked the operating system what I could do to become a better programmer. It said, "Let me give you some pointers."Commented:
object doesn't have many members.
It has member methods, but no properties.
kaufmed   ( ⚆ _ ⚆ )I asked the operating system what I could do to become a better programmer. It said, "Let me give you some pointers."Commented:
Also, people seem to mix up boxing/unboxing all of the time. The only time you run into boxing is when a value type (like int, byte, bool, etc.) gets shoved into a variable of type object. When does that happen, you say? One example would be if you assign directly:

object o = 5;

Open in new window


Another instance would be if you used a non-type safe collection like ArrayList. Everything in ArrayList is an object. ArrayList is not type safe at all, so you have to know what type the thing your retrieving is (kind of, but not exactly like dynamic). You can stick a string, an int, a duck, and a house all in the same ArrayList, and everything will compile, and everything will run...right up until you extract the house and try to make it quack...  This is why generic collections were created:  to give us (compile-time and run-time) type safety. Now with a generic list I cannot stick a duck inside of a house list.

Unboxing is simply the extracting of the actual value of the value type that got shoved into an object, and turning it back into a value-type variable.

e.g.

object x = 6;
int y = (int)x;

Open in new window


Boxing/unboxing is mostly related to how the data is stored in memory:  does it go on the stack or the heap. Value types are generally (speaking) stored on the stack; references types are on the heap. (This is an oversimplification, though.)

More information:  https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/boxing-and-unboxing

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CamilliaAuthor Commented:
The options provided are vauge in the interview question. I don't know which one is the correct one based on your explanations but it's ok.

Thanks for the boxing/unboxing explanation. I was reading about it before my interview. Your explanation is better than what I was reading.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.