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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:09:40+00:00 2026-05-12T11:09:40+00:00

Extract from CLR via C# on Boxing / Unboxing value types … On Boxing:

  • 0

Extract from CLR via C# on Boxing / Unboxing value types …

On Boxing: If the nullable instance is not null, the CLR takes the value out of the nullable instance and boxes it. In other words a Nullable < Int32 > with a value of 5 is boxed into a boxed-Int32 with a value of 5.

On Unboxing: Unboxing is simply the act of obtaining a reference to the unboxed portion of a boxed object. The problem is that a boxed value type cannot be simply unboxed into a nullable version of that value type because the boxed value doesn’t have the boolean hasValue field in it. So, when unboxing a value type into a nullable version, the CLR must allocate a Nullable < T > object, initialize the hasValue field to true, and set the value field to the same value that is in the boxed value type. This impacts your application performance (memory allocation during unboxing).

Why did the CLR team go through so much trouble for Nullable types ? Why was it not simply boxed into a Nullable < Int32 > in the first place ?

  • 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-12T11:09:41+00:00Added an answer on May 12, 2026 at 11:09 am

    I remember this behavior was kind of last minute change. In early betas of .NET 2.0, Nullable<T> was a "normal" value type. Boxing a null valued int? turned it into a boxed int? with a boolean flag. I think the reason they decided to choose the current approach is consistency. Say:

    int? test = null;
    object obj = test;
    if (test != null)
       Console.WriteLine("test is not null");
    if (obj != null)
       Console.WriteLine("obj is not null"); 
    

    In the former approach (box null -> boxed Nullable<T>), you wouldn’t get "test is not null" but you’d get "object is not null" which is weird.

    Additionally, if they had boxed a nullable value to a boxed-Nullable<T>:

    int? val = 42;
    object obj = val;
    
    if (obj != null) {
       // Our object is not null, so intuitively it's an `int` value:
       int x = (int)obj; // ...but this would have failed. 
    }
    

    Beside that, I believe the current behavior makes perfect sense for scenarios like nullable database values (think SQL-CLR…)


    Clarification:

    The whole point of providing nullable types is to make it easy to deal with variables that have no meaningful value. They didn’t want to provide two distinct, unrelated types. An int? should behaved more or less like a simple int. That’s why C# provides lifted operators.

    So, when unboxing a value type into a nullable version, the CLR must allocate a Nullable<T> object, initialize the hasValue field to true, and set the value field to the same value that is in the boxed value type. This impacts your application performance (memory allocation during unboxing).

    This is not true. The CLR would have to allocates memory on stack to hold the variable whether or not it’s nullable. There’s not a performance issue to allocate space for an extra boolean variable.

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

Sidebar

Related Questions

I would like to extract from a general HTML page, all the text (displayed
I'll regularly get an extract from a DB/2 database with dates and timestaps formatted
I need to take a paragraph of text and extract from it a list
I need to extract data from a .mpp file on the network and combine
When I extract files from a ZIP file created with the Python zipfile module,
I'm try to extract info from a MySQL DB into a MS SQL DB.
I want to use Perl to extract information from a Certificate Signing Request ,
I have barcode images in jpg format and want to extract barcode # from
I have written a few Python tools in the past to extract data from
I extract the audio from a bunch of flv files as an MP3. This

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.