This is my blog about coding in C#. C# is what I do, so C# is what I am going to talk about.
Monday, May 12, 2014
Where is the value of that?
Section 1.3 of the C# Language Specification 5.0 (for .NET 4.5) contains a discussion of types and variables. The two different types in C# that we have to play with are value types and reference types. Value types are those that have memory allocated for them to store values directly in, and conversely, reference types have memory allocated to store a reference to where the actual data is stored in memory. In other languages, we would call reference pointers, but since C# doesn't have pointers we call them reference types (huh?). Seriously though, you can't directly set or manipulate the value in the memory allocated to a reference type, you can only use it to access the value stored in the memory it points at. In an earlier section we mentioned that C# has a unified type system which means both value types and reference types are children of object, the base class for all. In reality, there is just some compiler/language magic that happens when we want to deal with value types as if they were objects. It is called boing, and it happens automatically to host the value type within an instance of an object. When we are through, we can unbox the value type and the language will ensure that we are unboxing the value into the correct value type variable.