blogs

miércoles, 22 de julio de 2015

Boxing and Unboxing

The System,Object class:
It is the reference type more important en .Net Framework,
All classes are a specialization  of the System.object type.
One variable of this type can refer to one reference of anyone other type.
On variable of this type can reference a anyone value type.
The reserve word is Object, it is an alias for System.object

Two views of the same object in memory



Boxing
Boxing is the act of converting a value type to a object type(reference type, this admit null values).
boxing is computationally expensive.
Value type do not contain  null values. examples bool, int,struct, enum, int, datetime,etc.

For Example:

In other examples I explained about the struct, value type and reference type , review this  post http://marisolca.blogspot.com/2015/05/struct-and-enum.html
and I used this class of this post http://marisolca.blogspot.com/2015/06/interface.html


Examples of Boxing



Run test






Unboxing

Unboxing is the reverse the boxing.
It is a explicit  convert  of the object type  to a value type that implement an interface
unboxing requires a cast.


Casting
programmer  specify option of the referenced data  for an object is of a  type specified and that it is sure  to reference  to this object using that type.
If the type of the object in memory does not match with specified in the cast,in the runtime will launch an exception.

For example:



 Run Test


IS and AS
C# provides us with the IS operator
Verify if the type is of an object is compatible with other type, value type or reference type

For example: 
Using IS operator






The AS operator is used for converter a type  an other different

For example:
Using AS Operator



No hay comentarios:

Publicar un comentario