Am I understanding correctly about boxing and unboxing?

兰树豪 381 Reputation points
2022-11-01T07:30:05.297+00:00

Am I understanding correctly about boxing and unboxing? pls see the picture , underline 1 and 2255933-boxingunboxing.jpg

Developer technologies | Windows Presentation Foundation
Developer technologies | .NET | Other
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Hui Liu-MSFT 48,681 Reputation points Microsoft External Staff
    2022-11-01T09:06:29.68+00:00

    Boxing is the process of converting a value type to the type object or to any interface type implemented by this value type. When the common language runtime (CLR) boxes a value type, it wraps the value inside a System.Object instance and stores it on the managed heap. Unboxing extracts the value type from the object. Boxing is implicit; unboxing is explicit. You could refer to Boxing and Unboxing (C# Programming Guide) for more details.

    1. myClassA.test(myClassA);
      myClassA is of type MyClassA, which is an object. The myClassA as an obj of type object in the test method does not belong to boxing.
    2. MyClassB myClassB = obj as MyClassB;
      Convert obj(myClassA) to MyClassB. It's not unboxing.

    Here is an example about boxing and UnBoxing.

        internal class Program  
        {  
            static void Main(string[] args)  
            {  
                MyClassA myClassA = new MyClassA();  
                MyClassB myClassB = new MyClassB();  
                myClassB.MyProp = 3;  
                myClassA.test(myClassB.MyProp); // called "Boxing"  [myClassB.MyProp is a int type  -- convert to --> test (object obj)  object type  ]  
          
            }  
        }  
        class MyClassA  
        {  
              
            public void test (object obj)  
            {  
                MyClassB myClassB = new MyClassB();  
                myClassB.MyProp =(int)obj;// called "UnBoxing"  [ obj is a object type --convert to -- > myClassB.MyProp is int type   ]  
            }  
        }  
        class MyClassB  
        {  
            public int MyProp { get; set; }  
        }  
      
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 78,236 Reputation points Volunteer Moderator
    2022-11-01T20:30:02.593+00:00

    the base class of all classes is "object". value types do not have a base class. so if you want to pass a value types as "object", you need to convert it to an object, this is called boxing.

    object boxed = 2; // box an int value
    int unboxed = (int) boxed; // unbox a boxed int

    the main use case for boxing is pass a value type as an object, say like:

    void Print(object o) => Console.WriteLine(o.ToString());
    ..

    Print(2); //box 2 to pass as an object, which Format() is expecting

    note: value types are allocated on the stack, and class instances are allocated in the heap. this means boxing a value type requires a heap allocation and garbage collection when released. this explains the overhead of boxing/unboxing.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.