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 6562159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:40:34+00:00 2026-05-25T13:40:34+00:00

Boxing converts a value type to an object type. Or as MSDN puts it,

  • 0

Boxing converts a value type to an object type. Or as MSDN puts it, boxing is an “operation to wrap the struct inside a reference type object on the managed heap.”

But if you try to drill into that by looking at the IL code, you only see the magic word “box.”

Speculating, I guess that the runtime has some sort of generics-based secret class up its sleeve, like Box<T> with a public T Value property, and boxing an int would look like:

int i = 5;
Box<int> box = new Box<int>;
box.Value = 5;

Unboxing the int would be far cheaper: return box.Value;

Unfortunately, my performance-hungry server application does a fair bit of boxing, specifically of decimals. Worse, these boxes are short-lived, which makes me suspect I pay twice, once for instanciating the box and then again for garbage collecting the box after I’m done with it.

If I was alloacting this memory myself, I would consider the use of an object pool here. But since the actual object creation is hidden behind a magic word in the IL, what are my options?

My specific questions:

  • Is there an existing mechanism for inducing the runtime to take boxes from a pool rather than instanciating them?
  • What is the type of the instance created during boxing? Is it possible to manually take control of the boxing process, yet still be compatible with unboxing?

If that last question seems strange, what I mean is that I could create my own Box<T> or DecimalBox class, pool it, and box/unbox manually. But I don’t want to have to go and modify the various places in the code that consume the boxed value (aka unbox it).

  • 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-25T13:40:34+00:00Added an answer on May 25, 2026 at 1:40 pm

    Speculating, I guess that the runtime has some sort of generics-based secret class up its sleeve

    Your speculation is almost right. Logically you can think of a box as being a magical Box<T> type that behaves as you describe (with a few more bits of magic; for instance, the way that nullable value types box is a bit unusual.) As an actual implementation detail, the runtime does not do it with generic types. Boxing existed in CLR v1, which was before generic types were added to the type system.

    my performance-hungry server application does a fair bit of boxing, specifically of decimals.

    If it hurts when you do that then stop doing that. Rather than trying to make boxing cheaper, stop doing it in the first place. Why are you boxing a decimal?

    Worse, these boxes are short-lived, which makes me suspect I pay twice, once for instanciating the box and then again for garbage collecting the box after I’m done with it.

    Short-lived is better than long lived; with short-lived heap objects you pay to collect them once and then they’re dead. With long-lived heap objects you pay that cost over and over again as the object continues to survive.

    Of course, the cost you are probably worried about regarding short-lived objects is not the cost of collection per se. Rather, it is the collection pressure; more short-lived objects allocated equals more frequent garbage collections.

    The allocation cost is pretty minimal. Move a pointer on the GC heap, copy the decimal into that location, done.

    If I was alloacting this memory myself, I would consider the use of an object pool here.

    Right; you pay the cost of collecting the long-lived object more, but you do fewer collections total because less collection pressure is produced. That can be a win.

    Is there an existing mechanism for inducing the runtime to take boxes from a pool rather than instanciating them?

    Nope.

    What is the type of the instance created during boxing? Is it possible to manually take control of the boxing process, yet still be compatible with unboxing?

    The type of the box is the type of the thing being boxed. Just ask it by calling GetType; it’ll tell you. Boxes are magical; they are the type of the thing that they contain.

    Like I said before, rather than trying to make boxing cheaper, just don’t do it in the first place.

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

Sidebar

Related Questions

By Definition we know that Boxing – Converts value-type TO reference type. Stored on
MSDN docs say that only value types need boxing, but this does not apply
I'm wondering whether boxing a value type in an object is a special case
Boxing is when a value type is assigned to an object type. Is it
I understand that boxing and unboxing is about casting (real type to object... object
Extract from CLR via C# on Boxing / Unboxing value types ... On Boxing:
I am trying to convert the value of the generic type parameter T value
I understand that auto un-boxing should be done with care because the reference that
I'm having a hard time understanding when to use Object (boxing/unboxing) vs when to
In WPF do DependencyProperty's cause lots of boxing/unboxing when used with value types? Or

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.