Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • What is Auto Boxing

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 553
    Comment on it

    Auto-boxing was introduced for removing the need to manually "box" primitives. It just happen automatically when and as needed.

    Auto-boxing can be defined as getting a reference type out of a value type by type conversion, it can be either implicit or explicit. To accomplish this, the compiler automatically supplies the extra source code which creates the object.

    Auto-boxing is mainly used for Garbage-Collection. It is recommended to use auto-boxing while developing the application as it allows the value type to be stored on the garbage collected heap and because of this, the system runs its own garbage collection and no memory is allocated at head area.

    In addition, C# doesn't support automatic unboxing since it doesn't have a separate set of "primitive type"' and "object types", i.e. in simple words, automatic boxing does not downcast automatically, e.g. the following code won't compile:-

    int iVal1 = 10;
    object obj = iVal1;   // box
    int iVal2 = obj;        // unbox (ERROR)
    Console.WriteLine(iVal2); // unreachable line, programmer might have expected output "10"
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: