C# provides us with value types and Reference
Types. Value Types are stored on the stack and
Reference types are stored on the heap. The conversion of value
type to reference type is known as boxing and converting reference type
back to the value type is known as unboxing.
Value
Type
Value types are primitive
types that are mapped directly to the FCL. Like
Int32
maps to
System.Int32,double
maps to
System.double. All value types are stored on stack and all the
value types are derived from System.ValueType. All structures
and enumerated types that are derived from System.ValueType are
created on stack, hence known as ValueType.
Reference Types
Reference Types are different
from value types in such a way that memory is allocated to them from the
heap. All the classes are of reference type. C#
new operator
returns the memory address of the object.
Boxing
Example
int Val = 11;
object Obj = Val; // boxing
The first line we created a Value Type Val and assigned a value to Val. The second line , we created an instance of Object Obj and assign the value of Val to Obj. The above operation (object Obj = i ) we saw converting a value of a Value Type into a value of a corresponding Reference Type . These types of operation is called Boxing
UnBoxing Example
int ValUnBox = Obj; // unboxing
The first line (int ValUnBox = (int) Obj) shows extracts the Value Type from the Object . That is converting a
value of a Reference Type into a value of a Value Type. This operation is called UnBoxing
Captions
- Asp.Net (9)
- ASP.Net Tips (1)
- C# (7)
- Computer (1)
- Internet Explorer (5)
- Java Script (11)
- Shortcut Keys (4)
- SQL Programming - Common Mistakes (11)
- Sql Server (22)
- Sql Server Definitions (8)
- Sql Server Test (2)
- SSRS (2)
About Me
- Vasanth.S
- I'm just normal guy who like to travel, addicted to TV and Computer, Playing Computer Games and Watching Movies and many more. Have lot of idea and quote in life
My Blog List
-
-
Pass data from one page to another using post15 years ago