What is Boxing and Unboxing in C#?

Solution

Boxing -  Boxing is the process to convert value type into the Object type.

Unboxing  - Converting object type into the value type.

Example:

        
        public void BoxingTest()
        {
            int number = 16;
            //Boxing -- converting number in object
            object obj = (object)number;

            //Unboxing -- converting object obj back to value type and printing on console
            Console.WriteLine((int)obj);
        }

Output

16

Share this thread

Comments

Comments
comments powered by Disqus

Navigation

Social Media