Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3793958
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:51:21+00:00 2026-05-19T12:51:21+00:00

Possible Duplicates: Why do we need boxing and unboxing in C#? What is boxing

  • 0

Possible Duplicates:
Why do we need boxing and unboxing in C#?
What is boxing and unboxing and what are the trade offs?

In C# what does "Box and Unbox" mean?

Here’s an extract from MSDN where I found the text.

But this convenience comes at a cost.
Any reference or value type that is
added to an ArrayList is implicitly
upcast to Object. If the items are
value types, they must be boxed when
they are added to the list, and
unboxed when they are retrieved. Both
the casting and the boxing and
unboxing operations decrease
performance; the effect of boxing and
unboxing can be very significant in
scenarios where you must iterate over
large collections.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T12:51:22+00:00Added an answer on May 19, 2026 at 12:51 pm

    Here is a more detailed explanation that looks at the internal of Common Language Runtime.

    First, let’s make the difference between value types and reference types:

    • A value type is held on the stack and a copy of it is passed to called methods
    • A reference value is held in the managed heap and the stack holds only a pointer (reference) to its location. The location, and not the object, is passed to called methods

    If you don’t know what the stack is (don’t be offended), it’s a memory area that holds local variables in a method and addresses of caller functions used for return instruction (just to be brief and provide a general answer). When you call a method, a sufficient area on the stack is statically allocated to it, so stack allocation is always called static allocation.

    The heap, instead, is a memory area separated from the stack, property of the running process, in which allocation must be first demanded to the operating system, and that’s why it’s called dynamic allocation (if you don’t run in an if statement, for example, memory may not be allocated for your process, instead stack is always allocated).

    Just to make a final example on heap and stack: in languages such as C++, declaring int[100] a; statically allocates 100*8 bytes on the stack (64-bit system assumed), while int* a = new int[100]; declares a 8 bytes (on 64-bit systems) area on the stack AND requests 800 more bytes on the heap, if and where available.

    Now let’s talk about C#:

    Boxing

    Since int is a value type, and is allocated on the stack, when you cast it to object or any other reference type (actually there is no other reference type from which int can inherit, but it’s a general rule) the value must become necessarily a reference type. So a new area on the heap is allocated, the object is boxed inside it and the stack holds a pointer to it.

    Unboxing

    Just the opposite: when you have a reference type, such as object, and want to cast it to a value type, such as to int, the new value must be held on the stack, so CLR goes to heap, un-boxes the value and copies it to the stack.

    In other words

    Remember the int[] and int* examples? Simply, when you have int in C#, the runtime expects its stack location to hold the value but instead when you have object, it expects its real value to be in the heap location pointed by the stack.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicates: Why does null need an explicit type cast here? Nullable types and
Possible Duplicate: What is boxing and unboxing and what are the trade offs? Ok
Possible Duplicates: Do Hibernate table classes need to be Serializable? What does Serializable mean?
Possible Duplicates: What is a framework? What does it do? Why do we need
Possible Duplicates: What does OOP mean What's the point of OOP? i am a
Possible Duplicates: Difference between char *str="STRING" and char str[] = "STRING"? Need some help
Possible Duplicate: Why XML-Serializable class need a parameterless constructor Does anyone know if it
Possible Duplicate: Get all files from VSS for a given date? I need to
Possible Duplicates: iPhone development on PC iPhone development on Windows Do I really need
Possible Duplicates: How do I extract HTML content using Regex in PHP RegEx match

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.