C# Interview Questions – value vs reference types

I’ve been conducting a lot of interviews lately for work. One of the questions that has grown over on me over time is to ask a C# developer “explain the difference between a value type and a reference type”. I  used to not think very highly of this question since virtually all my C# coding uses reference types exclusively. Over time I’ve come to see that developers that can answer this question well, can do so because they have looked into how the nitty gritty of C# works. This inquisitive nature is exactly what I want in developers on my teams.

Without further preamble,

What is the difference between a value type and reference type in C#?

Basics: value types are structs and reference types are classes.

Intermediate: value types are allocated on the stack and reference types are allocated on the heap.

Advanced: value types are allocated on the stack when they’re locally instantiated. Static value types and value types which are member fields of classes are both allocated on the heap!

That’s it! And that concludes my first post!

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

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