• +91 9723535972
  • info@interviewmaterial.com

CSharp Interview Questions and Answers

Question - Explain the concept of boxing and unboxing of the value type and object type in C#.

Answer -

Boxing- is a process of converting a value type to an object type where value type is placed on the stack memory, and the object type is placed in the heap memory. This conversion is an implicit conversion and you can directly assign any value to an object, and C# will handle the rest of the conversion on its own.

Example:

public void function()
{
Int a=111;
Object b=a; //implicit conversion
Console.WriteLine(b);
}
Unboxing- it is the reverse process of the boxing process. It is a conversion of the object type to the value type and the value of the boxed object type placed on the heap memory which will be transferred to the value type which is placed on the stack. This conversion of the unboxing process has to be done explicitly.

Example:

public void function()
{
Object b=111;
Int a=(int)b; //implicit conversion
Console.WriteLine(a);
}

Comment(S)

Show all Coment

Leave a Comment




NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners