What is autoboxing in C# ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Amar Kapasi

    #1

    What is autoboxing in C# ?

    What is autoboxing in C# ?
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    If I recall correctly, Autoboxing is a Java concept. Normal "boxing" was primitive types being converted to their respective object types. E.g:
    [code=java]
    int prim = 123;
    Integer obj = (Integer)prim;
    [/code]
    Autoboxing was introduced later to automate that, removing the need to manually "box" primitives. It would just happen automatically when and as needed.

    As far as I know, C# doesn't need this as primitives are basically just aliases for the object types to begin with.

    Unless you are talking about "boxing" as in turning things into Object instances, to be stored in hash tables and such. E.g:
    [code=cs]
    Int32 d = 123;
    Object p = (Object)d;
    [/code]

    P.S.
    Been a while since I really used either Java or C# so I might be a bit rusty xD

    Comment

    • Curtis Rutland
      Recognized Expert Specialist
      • Apr 2008
      • 3264

      #3
      I feel like we're answering a homework question here, Atli. But what the hell.

      Boxing and Unboxing in C#

      For the record, I've never heard of "autoboxing ." I looked it up:


      It's pretty much what you said. And you're right, C# doesn't require that, since the primitives are objects that can be referenced. They are aliases to their object types.

      Comment

      • Yuvraj S Jain

        #4
        Autoboxing is required in C# as the fundamently the concepts is briefly explained by you but the main thing to use the auto boxing is GC(Garbage Collection) as autoboxing will allows the value type to be stored on the garbage collected heap so that the system runs its own GC at that time no more memory will be there at head area...

        Comment

        • yyuuvvii
          New Member
          • Feb 2010
          • 5

          #5
          autoboxing will allows the value type to be stored on the garbage collected heap so that you have not any issue remains with memory problem you both are true but i recommended to use the autoboxing in C# while developing your application..

          Comment

          Working...