Difference between boxing and unboxing in C#
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jignesh KumarRank #91 · 22.9K reputationPosted Oct 31, 2022, 3:05 AM EDT
Boxing is process of converting value type variable to reference type varaiable or object.
i.e.
int myNum = 10; // 10 will assigned to myNum Object Obj = myNum; // BoxingUnboxing revers of boxing. It means converting Referecen type varialbe to value type.
i.e.
int myNum = 10; // value type is int and assigned value 10 Object Obj = myNum; // Boxing int i = (int)Obj; // UnboxingRajanikant HawaldarRank #110 · 20.2K reputationPosted Oct 31, 2022, 4:47 AM EDT
Object type refers to the value type, process of retrieving value from the boxed object.
The value stored on the stack is copied to the object stored on heap memory,The object's value stored on the heap memory is copied to the value type stored on stack.
Implicit conversion,Explicit conversion.
Boxing Example:
Unboxing Example:
int n = 24; object ob = n; // Boxing int m = (int) ob; // Unboxing