Difference between Boxing and Unboxing with Example

0
1741
difference between boxing and unboxing in c#.net

Difference between boxing and unboxing in C#

C# programming is a strongly-typed language. That is crucial idea you at all times get requested in your job interviews that distinction between boxing and unboxing in c#.internet.

Boxing is storing a worth kind as an object on the heap, and unboxing is studying the worth from the item. You’ll be able to solely unbox the worth because it’s a precise kind.

Casting is while you convert a fundamental kind to a different fundamental kind (like from an int to an extended), or while you change the kind of a reference (like from Record<int> to IEnumerable<int>)

  • C# Program to Discover Largest Quantity in an Array

Boxing and Unboxing in C# Interview Questions

Boxing and Unboxing is a vital idea in DotNet’s kind system. With Boxing and Unboxing one can hyperlink between value-types and reference-types by permitting any worth of a value-type to be transformed to and from kind object.

Boxing Unboxing
Definition: Boxing is the method of changing a worth sorts to the reference sorts. Unboxing is the method of changing a reference kind to worth sorts

.

Kind of Conversion: Implicit Conversion Specific Conversion
C# Example: int i = 221;

object obj1 = i; //boxing

object obj2 = 213;

i = (int)obj2 ; // unboxing

 

For extra data, see the C# Language Specification.

Did you discover this publish helpful? I hope you preferred this text. Please share with me your useful recommendations and suggestions.

LEAVE A REPLY

Please enter your comment!
Please enter your name here