Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

difference between boxing and unboxing in c#

/*
Boxing: The process of converting a value type instance to an object refrence.
When boxing happens CLR creates an object in the Heap and then creates a
a reference in the stack. So the value stroed in the heap along with an 
object refernce in the stack when boxing.
*/
//boxing:
int a = 10;
object obj = a;
// or 
object obj = 10;

/*
Unboxing: Is the opposition of boxing; Now when we cast an object to an integer
unboxing happens and the result is we get a new variable on the stack called
number with value of 10. 
*/
object obj = 10;
int number = (int) ob;
/*
Both boxing and unboxing have a performance penalty because of that extra 
object creation. And that's someting you should avoid if possible.
It's better to use a generic implemention of that class if it exists.
*/
Comment

boxing and unboxing in c#

class BoxingUnboxing
{  
    static void Main()  
    {  
        int myVal = 1;  
        object obj = myVal; // Boxing  
        int newVal = (int)obj; // Unboxing  
    }  
}  
Comment

PREVIOUS NEXT
Code Example
Csharp :: get gameobject layermask 
Csharp :: double quotes in a string c# 
Csharp :: C# random multiple of 5 in range 
Csharp :: add header in action asp.net mvc 
Csharp :: c# 10 null checl 
Csharp :: vb.net drag window without titlebar 
Csharp :: unity navmeshagent set destination 
Csharp :: string.insert c# 
Csharp :: sends keys enter selenium c# 
Csharp :: How to set default page asp.net MVC 
Csharp :: Generic Stack in c# 
Csharp :: c# convert bool to string 
Csharp :: how to show a first item in a combobox in c# winforms 
Csharp :: how to move mouse with c# 
Csharp :: delete items in c# 
Csharp :: dbset 
Csharp :: c# distinct comparer multiple properties 
Csharp :: How to create a class and objects in C# 
Csharp :: c# exit foreach 
Csharp :: c# get enum name from value 
Csharp :: muovere un elemento in c# 
Csharp :: How do I call a string inside a different class 
Csharp :: .net 3.1 bind json config 
Csharp :: O thread de chamada não pode aceder a este objecto porque existe outro thread que já o tem 
Csharp :: infinit range loop c# 
Csharp :: how to get point of collision in unity 
Csharp :: how to increase alpha in strings using unity 
Csharp :: autho close in persian time picker 
Csharp :: get datacontext of itemscontrol item c# 
Csharp :: c# add field to expando object 
ADD CONTENT
Topic
Content
Source link
Name
1+8 =