Saturday, October 24, 2015

Implicit Vs Explicit Data Types Conversion & Boxing Vs Unboxing

Implicit Conversion: Type safe conversion that not required any special syntax. No data lose because its type safe conversion.

//Implicit conversions
int i = 10000;
double d = i;
           
DerivedType objD = new DerivedType();
BaseType objB = objD;

Explicit Conversion: Converting or casting one datatype to another data type using same casting operators is called Explicit conversion.
This conversion may succeed or not.
This conversion may lose the data.

//Explicit conversions
int j = 1;
bool b = Convert.ToBoolean(j);
           
float f = 232.32f;
int k = (int)f;
                       
BaseType objBase = new BaseType();
DerivedType objDrived = (DerivedType)objBase;

-------------------------------------------------------------------------------
Value Type Vs Reference Type
When a variable is declared using one of the basic, built-in data types or a user defined structure, it is called a Value type. string is an exception because its basic data type but it is reference type.

Instances, arrays kind of types are Reference type

Lets checkout how .net stores these types in memory. How below given variables will be stored.
    //Boxing (Implicit conversion)
    int i = 2;         //1  
    object obj = i;    //2

  1. i is value type so stored in memory allocated in managed stack
  2. New reference type created and stored in stack, in stack memory it contain address(1221) of actual data. Memory allocated in managed heap for new created reference type 'obj' to store actual value. assigned value from i that is 2 is stored here.

    int j = 5;         //3
    obj = j;           //4

    //Unboxing (Explicit conversion)
    int k = (int)obj;  //5
  1. New value type j stored in stack
  2. j is assigned to 'obj', so new memory location(3421) allocated in heap to store new assigned value from j that is 5. Now address contained by obj is changed to new location 3421.
  3. Memory location 1221 is still there and it will be reclaimed by GC later.
  4. New value type k is created and allocated in stack, and assigned value from obj that is 5.
     k = i;  //6
  1. Value of k is overridden by value of i
Point to be noted: 
  1. When assign new value to a value type, value overridden and not required new memory allocation.
  2. As execution of memory block completed where the value type is declared, memory get released. As closing parenthesis executed memory used by ID will be released.
            private void SendSMStoClient()
            {
                int ID = 50;
            }
  3. When we assign new value to a reference type new memory allocated, and old one still exist there that is reclaimed by GC later. that is memory & performance overhead with reference types.
  4. Reference types are useful to pass them one function to other. While when we pass value type new copy passes of value type.
-------------------------------------------------------------------------------

Boxing: Converting value type to reference type. This is implicit conversion.
Unboxing: Converting reference type to value type. This is explicit conversion.

    //Boxing (Implicit conversion)
    int i = 2;
    object obj = i;

    //Unboxing (Explicit conversion)
    int k = (int)obj;

Why Boxing and Unboxing downgrades performance?
There is specific storage system for value types and for reference types.
Value types are stored in stack.
Reference types' reference pointer (address) is stored in stack but actual data stored in heap.
So whenever we perform Boxing or Unboxing .net internally changes the storage of data that is overhead so it downgrade performance.

No comments:

Post a Comment

CI/CD - Safe DB Changes/Migrations

Safe DB Migrations means updating your database schema without breaking the running application and without downtime . In real systems (A...