Why .Net Value Type Reference Type?

Ricky White
Ricky White used Ask the Experts™
on
In .Net, Why should a programmer care so much for knowing about Value Type and Reference type? Isn't it something Microsoft should have taken care of behind the scenes?

Is it necessary to know about them only to clear interviews? or is there a practical use?

Please advise.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2015
Commented:
It's a very important concept, that exists in most modern languages, although it could be presented under another name. In a properly designed framework, beginners can usually write their simple programs without taking care of the differences. I have given training to programmers who have programmed for years without even knowing that this difference existed. And a lot of pros are not even away of little details in the different ways that they behave.

First of all, if you look closely, you will find that most of the time, Value types are small variables, either a single value (Integer, Date) or a couple of properties (Size, Point), while Reference variables are usually bigger variables with many properties (TextBox, DataTable, FileStream). Reference variables thus usually require more memory. Contrary to what you think, Microsoft did take care of that behind the scene. Because Value types are small and Reference types are bigger, they are not handled the same way in memory, so as to optimize their use. Stack vs Heap if you learned C or C++ at some point.

Also, by default, they won't react the same way when you pass them to a method. When you pass a Value type, such as an Integer, the value is usually passed as a copy of the variable (there is an exception, lookup ByRef in the documentation). This is ususally what you want with an Integer. Most of the time, if you pass 5 to a method, it is so that the method works with the value 5, not the variable x that you used when you made the call. You would not want the method to change the value of x behind your back.

If you pass a reference type however, say a TextBox, most of the time you do not want to pass a copy, you want to pass the TextBox itself so that the method can act on it. Reference variables end up being passe that way. It's not the values of the TextBox that are passed to the method, it is its pointer (its address in memory), so that the method can act on the TextBox, not only on the values that it contains through its properties. The TextBox you pass can actually be modified by the method, because it has been passed by reference.

You see, it makes sense. A properly designed type will primarily be defined as a value or as a reference depending on it's intended use. And up to a certain point, more advanced programmers will also sometimes take into consideration the way the two different types of objects are handled in memory.
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

In .NET, there are two categories of types, reference types and value types.

Structs are value types and classes are reference types.

The general different is that a reference type lives on the heap, and a value type lives inline, that is, wherever it is your variable or field is defined.

A variable containing a value type contains the entire value type value. For a struct, that means that the variable contains the entire struct, with all its fields.

A variable containing a reference type contains a pointer, or a reference to somewhere else in memory where the actual value resides.

This has one benefit, to begin with:

•value types always contains a value
•reference types can contain a null-reference, meaning that they don't refer to anything at all at the moment
Internally, reference types are implemented as pointers, and knowing that, and knowing how variable assignment works, there are other behavioral patterns:

•copying the contents of a value type variable into another variable, copies the entire contents into the new variable, making the two distinct. In other words, after the copy, changes to one won't affect the other
•copying the contents of a reference type variable into another variable, copies the reference, which means you now have two references to the same somewhere else storage of the actual data. In other words, after the copy, changing the data in one reference will appear to affect the other as well, but only because you're really just looking at the same data both places
When you declare variables or fields, here's how the two types differ:

•variable: value type lives on the stack, reference type lives on the stack as a pointer to somewhere in heap memory where the actual memory lives
•class/struct-field: value type lives inside the class, reference type lives inside the class as a pointer to somewhere in heap memory where the actual memory lives.

Sources: http://stackoverflow.com/questions/13049/whats-the-difference-between-struct-and-class-in-net
Anuradha GoliSystems Development / Support Specialist
Commented:
You can get clear understanding of the concept from this link
http://www.codeproject.com/Articles/76153/6-important-NET-concepts-Stack-heap-Value-types-re

It is not only important to know the concept or implementation behind the scene not only for the interview,but for clear implementation of them in practical scenario
anarki_jimbelSenior Developer
Commented:
OK, I believe the topic starter knows concepts but asks "what for?!".

One reason is memory management. In C++ memory management is responsibility of a programmer. In .Net - framework takes care of. However, to write efficient code you need to know about reference and value types. See the free e-book. It is easy to read and understand:

http://www.reflector.net/2012/03/grab-free-copy-%E2%80%9Cunder-hood-net-memory-management%E2%80%9D/

Author

Commented:
Thanks all!

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