Sunday, July 8, 2018

What is Boxing and Unboxing

    public class Testing {
public Testing() {
}
 
public void Method() {
/* Another Comment
on multiple lines */
int x = 9;
}
}
// Comment
public class Testing {
public Testing() {
}
 
public void Method() {
/* Another Comment
on multiple lines */
int x = 9;
}
}

// Comment
public class Testing {
public Testing() {
}
 
public void Method() {
/* Another Comment
on multiple lines */
int x = 9;
}
}

int? i = null; int j = i ?? 0; Console.WriteLine(j);
What is Boxing and Unboxing?

Answer:Boxing and Unboxing both are used for type conversion but have some difference:

Boxing:

Boxing is the process of converting a value type data type to the object or to any interface data type which is implemented by this value type. When the CLR boxes a value means when CLR is converting a value type to Object Type, it wraps the value inside a System.Object and stores it on the heap area in application domain.

Example:



Unboxing:

Unboxing is also a process which is used to extract the value type from the object or any implemented interface type. Boxing may be done implicitly, but unboxing have to be explicit by code.

Example:



The concept of boxing and unboxing underlines the C# unified view of the type system in which a value of any type can be treated as an object.
Share:

0 comments:

Post a Comment

Developer Technology

Developer Technology

Upcoming Articles/Videos

Data Abstraction and Encapsulation Video
When to use Abstract class in C#
Copyright © .NET Developer | Powered by Shrimant Telgave Design by Shrimant Telgave