Difference between Boxing and Un-Boxing in C#.net

Category: C#.NetOOPS Comments: No comments

Boxing and Un-Boxing

Boxing: means converting value-type to reference-type.


int I = 20;

string s = I.ToSting();


UnBoxing: means converting reference-type to value-type.


int I = 20;

string s = I.ToString(); //Box the int

int J = Convert.ToInt32(s); //UnBox it back to an int.


Note: Performance Overheads due to boxing and unboxing as the boxing makes a copy of value type from stack and place it inside an object of type System.Object in the heap.

Leave a Reply

Your email address will not be published. Required fields are marked *


HTML tags are not allowed.