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 :: how to flip a character in unity 2d 
Csharp :: split string c# 
Csharp :: c# array.join 
Csharp :: c# string enum 
Csharp :: mfind how many digits a number has c# 
Csharp :: unity text color 
Csharp :: set rotation unity 
Csharp :: .net core partial view with model 
Csharp :: convert list of tuples to dictionary c# 
Csharp :: disable button in android studio 
Csharp :: c# change language version to 9.0 
Csharp :: bundle.config in mvc is missing 
Csharp :: c# excel close workbook 
Csharp :: how to remove all whitespace from a string in c# 
Csharp :: linq query get last day of month 
Csharp :: test how catch exception c# 
Csharp :: c# delete files in directory and subdirectories 
Csharp :: uri file path c# 
Csharp :: c# handle single quote inside string 
Csharp :: csharp get decimal part of number 
Csharp :: c# itext 7 PdfDocument from byte array 
Csharp :: C# fileinfo creation date 
Csharp :: how to store some variables on the device in unity 
Csharp :: replace multiple characters in string c# 
Csharp :: how read excel data in c# 
Csharp :: c# nullable generic 
Csharp :: c# linq to select even numbers 
Csharp :: c# get date without time 
Csharp :: microsoft forms create bitmap 
Csharp :: how to get mouse position c# 
ADD CONTENT
Topic
Content
Source link
Name
7+9 =