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(); } } }
Liked By
Write Answer
Can you tell me about basic concept of boxing and unboxing?
Join MindStick Community
You have need login or register for voting of answers or question.
Elena Glibart
14-Sep-2016