Saturday 20 July 2013

Difference between Value Type and Reference Type :

In simple words, all value based types are allocated on the stack, while all reference based types are allocated on the heap. What does this mean? A value type contains the actual value. A reference type contains a reference to the value. When a value type is assigned to another value type, it is copied. When a reference type is assigned to another reference type, a reference is assigned to the value.

By saying stack, we mean things are kept one on top of the other. We keep track of each value at the top. By saying heap, we mean things are kept in a mashed order. We keep track of each value by its address, that is referenced by a pointer to it.

All value types are implicitly derived from System.ValueType. This class actually overrides the implementation in System.Object, the base class for all objects which is a reference type itself.

Data types like integers, floating point numbers, character data, Boolean values, Enumerations and Structures are examples of Value Types. Classes, Strings, Arrays are examples of Reference Types.

A value type may not contain NULL values. Reference types may contain NULL values.

It is not possible to derive new types from Value Types. This is possible in Reference types. However, Value Types like Structures can implement interfaces. 
Value type Reference type
Value type they are stored on stack Reference type they are stored on heap
When passed as value type new copy is created and passed so changes to variable does not get reflected back When passed as Reference type then reference of that variable is passed so changes to variable does get reflected back
Value type store real data Reference type store reference to the data.
Value types are faster in access Reference types are slower in access.
Value type consists of primitive data types, structures, enumerations. Reference type consists of class, array, interface, delegates
Value types derive from System.ValueType Reference types derive from System.Object
Value types can not contain the value null. Reference types can contain the value null.

  • Structure
  • Become a member
  • Home
  • Primitive
  • Security Systems
  • Abstraction
  •  
  • In simple words, all value based types are allocated on the stack, while all reference based types are allocated on the heap. What does this mean? A value type contains the actual value. A reference type contains a reference to the value. When a value type is assigned to another value type, it is copied. When a reference type is assigned to another reference type, a reference is assigned to the value.

    By saying stack, we mean things are kept one on top of the other. We keep track of each value at the top. By saying heap, we mean things are kept in a mashed order. We keep track of each value by its address, that is referenced by a pointer to it.

    All value types are implicitly derived from System.ValueType. This class actually overrides the implementation in System.Object, the base class for all objects which is a reference type itself.

    Data types like integers, floating point numbers, character data, Boolean values, Enumerations and Structures are examples of Value Types. Classes, Strings, Arrays are examples of Reference Types.

    A value type may not contain NULL values. Reference types may contain NULL values.

    It is not possible to derive new types from Value Types. This is possible in Reference types. However, Value Types like Structures can implement interfaces. 

No comments:

Post a Comment

C# LINQ Joins With SQL

There are  Different Types of SQL Joins  which are used to query data from more than one database tables. In this article, you will learn a...