Value types and boxing

Published Thu, Sep 16 2010 22:39

As we’ve seen, value types have better performance than reference types because they’re allocated in the stack, they’re not garbage collected nor do they get the extra weight generally associated with reference types. There are, however, some times where we need a “reference to  a value object” (yes, I wanted to write “reference to a value object”). In the old days, that would happen whenever you needed a collection of value objects (as you recall, in .NET 1.0/.NET 1.1 there were no generics). Here’s a small example:

public struct Student {
    public String Name { get; set; }
    public Int32 Age { get; set; }
}

Now, and this is important, pay attention to the following snippet:

var cll = new ArrayList();
var std = new Student {Name = "Luis", Age = 10};
cll.Add(std);
var std2 = (Student)cll[0];
std2.Name = "Abreu";

If you look at the docs, you’ll notice  that the Add method expects an Object instance. In other words, it requires a reference type and not a value type. If you go ahead and compile the previous snippet, you won’t get any compilation errors. What’s going on here? What you’re seeing is perfectly normal and it’s called boxing. Boxing allows us to convert a value type into a reference type. Boxing involves a rather simple algorithm:

  1. allocate memory on the managed heap for the value object plus the “traditional” overhead space required for all the reference types (btw, I’m talking about the type object pointer and the sync block index).
  2. copy the values of the value type’s fields into the heap’s allocated memory.
  3. use the returned memory address as the “converted” reference type instance.

When the compiler detected that the Add method requires a reference type, it went ahead and applied the previous algorithm in order to transform the value type and pass a reference into the method. In other words, what got added to the cll collection was the reference obtained from step 3 (and not the std variable).

This has lots of implications which might not be obvious at first. For instance, if you think that the following snippet should print true, you’re wrong:

Console.WriteLine(std.Name == std2.Name);

Before getting into why that happens, we need to understand unboxing. Since it’s 22:37 and I still haven’t had my dinner, I’ll leave the unboxing post for later :)

Stay tuned for more!

Filed under: ,

Comments

# ASPInsiders said on Thursday, September 16, 2010 5:51 PM

Yes…well, it is, but it’s cheaper (as we’ll see). As we’ve seen in the previous post , we can convert

# LA.NET [EN] said on Friday, September 17, 2010 6:09 AM

[Update: Thanks to JPC for finding (yet!) another error in my post] Yes…well, it is, but it’s cheaper

# LA.NET [PT] said on Friday, September 17, 2010 6:09 AM

[Update: Thanks to JPC for finding (yet!) another error in my post] Yes…well, it is, but it’s cheaper

# LA.NET [EN] said on Friday, September 17, 2010 7:24 AM

In the previous posts , I’ve presented the basics on boxing and unboxing. Today, we’ll take a deep dive

Leave a Comment

(required) 
(required) 
(optional)
(required) 
If you can't read this number refresh your screen
Enter the numbers above:  

Search

This Blog

Tags

Community

Archives

Syndication

Email Notifications

News




  • View Luis Abreu's profile on LinkedIn


    Follow me at Twitter

    My books

    Silverlight 4.0: Curso Completo

    ASP.NET 4.0: Curso Completo

    Portuguese LINQ book cover

    Portuguese ASP.NET 3.5 book cover

    Portuguese ASP.NET AJAX book cover

    Portuguese ASP.NET AJAX book cover