Interview question - dynamic vs object

Camillia
Camillia used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
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.
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.
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
Commented:
object doesn't have many members.
It has member methods, but no properties.
Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
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
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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial