Implementation versus Behaviour


Another bugbear I’ve come across lately: Please answer the following question without mentioning the terms “heap” or “stack”: –

What’s the difference between .NET reference and value types?

I’ve asked a number of people this question lately, and, without fail, all of them have answered that the former are allocated on the heap and the latter on the stack. That is indeed one difference, but it’s an implementation detail. It could change tomorrow and your code would still behave in the same way. As a developer of a .NET language, it has a much smaller impact on how you write code than the real answer.

So what’s the real difference between the two? It’s how they behave when passed between methods i.e. value types are copied whereas reference types refer to the same instance.

Most everyone I prompt knows this, but do not think to say it when asked the original question.

Advertisements

One thought on “Implementation versus Behaviour

  1. I’ve read enough of Eric Lippert to know that heap/stack are indeed implementation details (and that not all value types get allocated on the stack).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s