Boxing is used to store value types into the object type. It is used in the program to store the value of unused variable. We can store the value of the variable with the datatype into object type so that variable can be useful for the rest of the program.
Syntax : int i = 123; object o = i; Program related to Boxing; class TBoxing {
static void Main() {
int i = 123;
object o = i; // boxing concept
i = 456;
System.Console.WriteLine("The value-type value = {0}", i);
System.Console.WriteLine("The object-type value = {0}", o); } } Unboxing: Unboxing is used to get the value
types from the object type. the datatype must be same for the variable that we
have assigned during boxing. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication9 {
class Program {
static void Main(string[] args)
{
int i = 123;
object o = i; // boxing concept
i = 456;
int j = (int)o;//unboxing concept
Console.WriteLine("The value-type value = {0}", i);
Console.WriteLine("The value-type value = {0}", j);
Console.ReadLine();
} } }
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Can you answer this question?
Write Answer1 Answers