Terms: Value Types vs. Ref Types

While searching for an answer about how to tell a type is ref or value, I came through this blog – "Value Types, Reference Types, and writing with clarity!", which is quite concise and practical, though my question is not answered here.

All .NET Framework data types are either value types or reference types.
Value Types
Memory for a value type is allocated on the current thread’s stack. A value type’s data is maintained completely within this memory allocation. The memory for a value type is maintained only for the lifetime of the stack frame in which it is created. The data in value types can outlive their stack frames when a copy is created by passing the data as a method parameter or by assigning the value type to a reference type. Value types are passed by value by default . "By Value" is when an argument is passed into a function by passing a copy of the value. In this case, changing the copy doesn’t affect the original value,

If a value type is passed to a parameter of reference type, a wrapper object is created (the value type is boxed), and the value type’s data is copied into the wrapper object. For example, passing an integer to a method that expects an object results in a wrapper object being created.
Reference Types
The data for reference type objects is always stored on the managed heap. Variables that are reference types consist of only the pointer to that data. The memory for reference types such as classes, delegates, and exceptions is reclaimed by the garbage collector when they are no longer referenced. It is important to know that reference types are always passed by reference. "By Reference" is when an argument is passed to a function by passing a reference to the actual value. In this case, if you change the argument in the function, you also change the original.

If you specify that a reference type should be passed by value, a copy of the reference is made and the reference to the copy is passed *.


The answer to my question is, quoted from Applied Microsoft .NET Framework Programming, Looking up in MSDN.

The .NET Framework Reference documentation clearly indicates which types are reference types and which are value types. When looking up a type in the documentation, any type called a class is a reference type. For example, the System.Object class, the System.Exception class, the System.IO.FileStream class, and the System.Random class are all reference types. On the other hand, the documentation refers to each value type as a structure or an enumeration. For example, the System.Int32 structure, the System.Boolean structure, the System.Decimal structure, the System.TimeSpan structure, the System.DayOfWeek enumeration, the System.IO.FileAttributes enumeration, and the System.-Drawing.FontStyle enumeration are all value types.
If you look more closely at the documentation, you’ll notice that all the structures are immediately derived from the System.ValueType type. System.ValueType is itself
immediately derived from the System.Object type. By definition, all value types must be derived from ValueType.
All enumerations are derived from System.Enum, which is itself derived from System.ValueType. The CLR and all programming languages give enumerations special treatment. For more information about enumerated
types, refer to Chapter 13.
Even though you can’t choose a base type when defining your own value type, a value type can implement one or more interfaces if you choose. In addition, the CLR doesn’t allow a value type to be used as a base type for any other reference type or value type. So, for example, it’s not possible to define any new types using Boolean, Char, Int32, Uint64, Single, Double, Decimal, and so on as base types.
For many developers (such as unmanaged C/C++ developers), reference types and value types will seem strange at first. In unmanaged C/C++, you declare a type and then the code that uses the type gets to decide if an instance of the type should be allocated on the thread’s stack or in the application’s heap. In managed code,
the developer defining the type indicates where instances of the type are allocated; the developer using the type has no control over this.

Post a comment or leave a trackback: Trackback URL.

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

%d bloggers like this: