[Previous] [Contents] [Next]

Reference Types


Reference types are similar to references in C++ in that they are type-safe pointers. The type-safe part means that instead of merely being an address, which might or might not point to what you think it does, a reference (when not null) is always guaranteed to point to an object that is of the type specified and that has already been allocated on the heap. Also take note of the fact that a reference can be null.

In the following example, a reference type (string) is being allocated. However, what's happening under the covers is that the value has been allocated on the heap and a reference to that value has been returned.

string s = "Hello, World";

As with value types, there are several types defined as reference types in C#: classes, arrays, delegates, and interfaces. Any time you declare a variable of one of these types, you allocate the number of bytes associated with that type on the heap, and you are working with a reference to that object instead of directly with the bits (as with value types).

[Previous] [Contents] [Next]