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

The Archive Base Latest Questions

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

Why do we need boxing and unboxing in C#? I know what boxing and

  • 0

Why do we need boxing and unboxing in C#?

I know what boxing and unboxing is, but I can’t comprehend the real use of it. Why and where should I use it?

short s = 25;

object objshort = s;  //Boxing

short anothershort = (short)objshort;  //Unboxing
  • 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-13T13:33:40+00:00Added an answer on May 13, 2026 at 1:33 pm

    Why

    To have a unified type system and allow value types to have a completely different representation of their underlying data from the way that reference types represent their underlying data (e.g., an int is just a bucket of thirty-two bits which is completely different than a reference type).

    Think of it like this. You have a variable o of type object. And now you have an int and you want to put it into o. o is a reference to something somewhere, and the int is emphatically not a reference to something somewhere (after all, it’s just a number). So, what you do is this: you make a new object that can store the int and then you assign a reference to that object to o. We call this process “boxing.”

    So, if you don’t care about having a unified type system (i.e., reference types and value types have very different representations and you don’t want a common way to “represent” the two) then you don’t need boxing. If you don’t care about having int represent their underlying value (i.e., instead have int be reference types too and just store a reference to their underlying value) then you don’t need boxing.

    where should I use it.

    For example, the old collection type ArrayList only eats objects. That is, it only stores references to somethings that live somewhere. Without boxing you cannot put an int into such a collection. But with boxing, you can.

    Now, in the days of generics you don’t really need this and can generally go merrily along without thinking about the issue. But there are a few caveats to be aware of:

    This is correct:

    double e = 2.718281828459045;
    int ee = (int)e;
    

    This is not:

    double e = 2.718281828459045;
    object o = e; // box
    int ee = (int)o; // runtime exception
    

    Instead you must do this:

    double e = 2.718281828459045;
    object o = e; // box
    int ee = (int)(double)o;
    

    First we have to explicitly unbox the double ((double)o) and then cast that to an int.

    What is the result of the following:

    double e = 2.718281828459045;
    double d = e;
    object o1 = d;
    object o2 = e;
    Console.WriteLine(d == e);
    Console.WriteLine(o1 == o2);
    

    Think about it for a second before going on to the next sentence.

    If you said True and False great! Wait, what? That’s because == on reference types uses reference-equality which checks if the references are equal, not if the underlying values are equal. This is a dangerously easy mistake to make. Perhaps even more subtle

    double e = 2.718281828459045;
    object o1 = e;
    object o2 = e;
    Console.WriteLine(o1 == o2);
    

    will also print False!

    Better to say:

    Console.WriteLine(o1.Equals(o2));
    

    which will then, thankfully, print True.

    One last subtlety:

    [struct|class] Point {
        public int x, y;
    
        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }
    
    Point p = new Point(1, 1);
    object o = p;
    p.x = 2;
    Console.WriteLine(((Point)o).x);
    

    What is the output? It depends! If Point is a struct then the output is 1 but if Point is a class then the output is 2! A boxing conversion makes a copy of the value being boxed explaining the difference in behavior.

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

Sidebar

Related Questions

I know that boxing/unboxing affects performance. According to MSDN, unboxing can take four times
MSDN docs say that only value types need boxing, but this does not apply
Need to use own imaged markers instead built-in pins. I have several questions. 1.
This question might seem very specific but I am in need of some ideas
I have some tests that use guids. The guids used don't need to be
This is not a question of what is boxing and unboxing, it is rather
What I need to do is this: <iframe src=http://www.google.com width=800 height=600></iframe> But the constraint
I need to use java-legacy code with the following method: public void doit(Map <String,
In my company we use .def files to specify the symbols that need to
So I know there is this question addressing the need for getting the bytes

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.