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

  • Home
  • SEARCH
  • 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 8472577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:06:53+00:00 2026-06-10T17:06:53+00:00

To box a value type you cast it to System.Object – that in itself

  • 0

To box a value type you cast it to System.Object – that in itself seems “wrong” to me (because casting should either convert the value to another type (so converting an Int32 to Object should be a data-lossy action, as Object has no instance state of its own) or converting an interface pointer to a parent (which does not affect the state of the object being pointed to and is a compile-time concern). When boxing a value-type in the CLR you’re copying the value to the heap and losing interface information at the same time, when you only really want to perform the first task (copying the value to the heap, or at least getting a reference to it).

Java solves this problem with the strong types of Integer and Long. Why doesn’t .NET have this?

I have my own collection of utility source code that I like to include in other projects, and they include their own implementations of strongly-typed boxed classes (such as BoxedInt32) which override the implicit and explicit conversion operators so they work the same way as casting to Object does, except without having to actually cast to object (thus preserving type data). So I can do this:

private BoxedInt32 _reference;
public Int32 GetValue{ return _reference; }

So why doesn’t .NET, after four major releases, still not have strongly-typed boxed types?

  • 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-06-10T17:06:55+00:00Added an answer on June 10, 2026 at 5:06 pm

    There would be no point. You would never directly use BoxedInt32, since if you knew the type in advance it would only be sensible to use int instead of the box. So the only interesting case is: when we don’t know the type in advance, i.e. when dealing with object. Well, value-type boxing already handles that perfectly, reporting the type as Int32.

    The reason the specific boxed versions exist in Java is, in part, because Java generics work very differently via type erasure; however .NET has true generics including for value-types, so this is unnecessary.

    You could make your own box:

    public class Box<T> where T : struct {
        private readonly T value:
        public Box(T value) { this.value = value; }
        public T Value { get { return value; } }
    }
    

    (perhaps adding more features such as conversions / equality): but – again: no point whatsoever. Regular inbuilt boxing already handles this much better.

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

Sidebar

Related Questions

i am validating an input box value so that it should contain only digit,.,-,/
i have small function that running on condintion, if text box value is not
I have a select box of TYPES that each type has their own PARAMETERS.
I have a label that holds a value and a text box into which
How can i get the value that was pressed in the confirm box? <script
I'm wondering whether boxing a value type in an object is a special case
Boxing converts a value type to an object type. Or as MSDN puts it,
I want to know get text box value when I type the value by
I need to validate the text box value from the generated html table (grid)
I am attempting to pass a text box value into a SQL query, something

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.