Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Okay, there are several topics you're touching here. First of all, let's take a look at value types and why they exist. Value types are what you use when you need <em>value semantics</em>:</p> <blockquote> <p>With classes, it is possible for two variables to reference the same object, and thus possible for operations on one variable to affect the object referenced by the other variable. With structs, the variables each have their own copy of the data (except in the case of ref and out parameter variables), and it is not possible for operations on one to affect the other. Furthermore, because structs are not reference types, it is not possible for values of a struct type to be null.</p> </blockquote> <p>All numeric types, for example, are value types precisely because they need to have value semantics. If the variable <strong>x</strong> has value 17 and you assign <strong>x</strong> to <strong>y</strong>, then <strong>y</strong> will have its <em>own</em> value 17 and incrementing <strong>y</strong> won't change <strong>x</strong> to 18. Therefore, unless you have a good reason, use a struct <em>only</em> when defining a type that needs to have value semantics.</p> <p>At the implementation level, value semantics are enforced by using in-line allocation. You can read more about it <a href="http://www.albahari.com/valuevsreftypes.aspx" rel="nofollow noreferrer">here</a>.</p> <p>This leads us to boxing. When does boxing happen? When you cast a value type into a reference type. You used the type <strong>object</strong> as an example, but with C# generics, that's something that should happen rarely in practice. A more frequent case would be to cast a value type into an interface; for example, casting a <strong>double</strong> into an <strong>IEquatable</strong> or <strong>IComparable</strong>. At any rate, if you cast a valu type into a reference type, boxing will and must occur.</p> <p>What does really happen when boxing occurs? A <em>copy</em> of the instance to be boxed is made and placed on the heap, as an independent object, so that it can be safely referenced even when the original instance goes out of scope. If it wasn't for the boxing, it would be easy to get the CLR to try to access invalid memory and we all know that's a baaaaad thing.</p> <p>So, is boxing good or bad? On the one hand it's good, because it allows you to safely cast value types into reference types when you need it. On the other hand, it creates "litter" -- short-lived instances of objects that get discarded and add to the work the garbage collector has to do. Is this bad? Only in <a href="http://blogs.msdn.com/shawnhar/archive/2007/07/02/twin-paths-to-garbage-collector-nirvana.aspx" rel="nofollow noreferrer">some cases</a>, such as developing XNA games. If this is your case, you'll want to avoid uncontrolled boxing; if so, I would also invite you to stop by my blog where I have some <a href="http://beardseye.blogspot.com/search/label/boxing" rel="nofollow noreferrer">bits of advice on that topic</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload