Value types VS Reference types

Value types and Reference
types.

 

The .NET type system has two different kinds of types namely Value
types and

Reference types.

Value types directly contain the data,
and instances of value types are either

allocated on the stack or
allocated inline in a structure. Value types can be built-in

(implemented by the runtime),
user-defined, or enumerations.

The core value types supported by the .NET platform reside within
the root of the

System namespace. There types are often
referred to as the .NET “Primitive Types”.

They include:

Ø       Boolean

Ø       Byte

Ø       Char

Ø       DateTime

Ø       Decimal

Ø       Double

Ø       Guid

Ø       Int16

Ø       Int32

Ø       Int64

Ø       SByte

Ø       Single

Ø       Timespan

 

Reference types store a reference
to the value’s memory address, and are allocated on the heap. Reference types
can be self-describing types, pointer types, or interface types. The type of a
reference type can be determined from values of self describing

types. Self-describing
types are further split into arrays and class types.

 

Value Type vs. Reference Type

The primary difference between
reference and value types is how instances of the two types are treated by the
CLR. One difference is that the GC collects instances of

reference types that are no longer referenced by the application. Instances
of value types are automatically cleaned up when the variable goes out of
scope. Let’s take a

look at an example in
VB.NET:

 

Sub Test()

Dim myInteger
as Integer

Dim myObject
as Object

End Sub

‘myInteger a Value type is automatically cleaned up
when the Sub ends.

‘But myObject

a Reference type is not cleaned up until the GC is run.

 

 

Another difference is when one variable is set equal to another or
passed as a parameter to a method call. When a variable of a reference type (A) is set equal to

another variable of the same type (B), variable A is assigned a
reference to B. Both

variables reference the
same object. When a variable of value type (A) is set equal

to another variable of the same type (B), variable A receives a copy
of the contents

of B. Each variable
will have its own individual copy of the data.

 

Yet another difference between the behaviors of value types versus
reference types

is how equality is determined.
Two variables of a given reference type are

determined to be equal if
both the variables refer to the same object. Two variables

of a given value type are
determined to be equal if the state of the two variables are

equal.

 

The final difference between the two is the way the instances of a
type are initialized.

In a reference type, the variable is
initialized with a default value of Null. The variable will not reference an
object until explicitly done by the object. Whereas a variable declared as a
value type will always reference a valid object.

 

Custom Types

A Custom Type is a set of data and related behavior that is
defined by the developer.

A developer can define both custom
reference type and custom value types. In vb.net we can define custom types by
using the Structure keyword. Let’s look at

an example wherein we define a
custom value type.

 

Sub Test()

Dim myInteger as Integer

Dim myObject as Object

End Sub

‘myInteger a Value type is
automatically cleaned up when the Sub ends.

‘But myObject a Reference type is not cleaned up until the GC is
run.

 

We declare a structure by name Test, it signals vb.net compiler to have Test derive from System.ValueType and therefore a
value type. In the Main() we initialize x and then set y equal to x. Since x and y are both
instances of value types, y is set equal to the value of x. After changing the fields in y write the value
of the fields in both x and y to the Console. The output of the program is:

 

x: myInt = 4 and myString = Test

y: myInt = 1 and myString = Changed

 

Notice that even after changing the value of fields in y it did not affect
x. This is

exactly the behavior
required for primitive types.

 

Boxing and Unboxing Value Types

Sometimes it is required to treat an instance of a value type as
if it were an instance

of a reference type. An example of this is when a value type is
passed ByRef as a

parameter of a method.
This is where the concept of Boxing becomes important.

 

Boxing occurs when an instance of a value type is converted to a
reference type. An

instance of a value type can be converted either to a System.Object or to any other

interface type implemented
by the value type.

 

Module Module1

Public Structure
Test

Public myString as String

Public myInteger as Integer

End Structure

Public Sub Main()

‘Notice that
both declarations are equivalent

‘Both x and y
are instance of type test

Dim x as New Test()

Dim y as Test

x.myInteger = 4

y.myString = “Test”

‘Reference to x
is assigned to y

y = x

y.myInteger = 1

y.myString = “Changed”

Console.WriteKine(String.Format(“x : myInt =
{0} and String = {1} ”, _

x.myInteger, x.myString))

Console.WriteKine(String.Format(“y : myInt =
{0} and String = {1} ”, _

y.myInteger, y.myString))

End Sub

Module Module1

Public Function Add(ByVal x As Object, ByVal y As Object) As Object

Add = x + y

End Function

Public Sub Main

Dim x As Integer = 2

Dim y As Integer = 3

Dim sum As
Integer

Sum = Add(x , y)

Console.WriteLine(“ {0) + {1} =
{2} ”, x, y, sum)

End Sub

End Module

 

In the above example both x and y are boxed before they are passed to Add. Then x,y and Sum are boxed before
they are passed to WriteLine. Unboxing involves the conversion of
an instance of a reference type back to its original value type. In Vb.net it
is done using the helper functions in the Microsoft.VisualBasic.Helpers namespace. For
example in the above example, IntegerType.FromObject is called to unbox the return parameter of type object back to Integer.

More information about Common Type System can be obtained from

http://msdn.microsoft.com/library/en-us/cpguide/html/cpconcommontypesystemoverview.asp

 

 

Part of this article has been taken from India Community
Initiative – .NET Tutorial for Beginners.

 

Advertisements

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 )

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s