Last time, I discussed the ref type.  This time, I'm going to talk about the value type.  This is the CLR type exposed in Managed Extensions by the __value keyword, and available in Whidbey C++ using the context-sensitive keyword value.

What is a value type?  I view value types as a convenient way to lump data together.  This isn't to say it has no member functions - it might well - but the concept is of a chunk of data.  Where you might use a ref type to implement a linked list class, a value type might be the right choice for the nodes in that linked list.

What makes a value type different from a ref type?  Value types can go almost anywhere: on the stack, as members of a ref class, passed to functions, returned from functions, and even on the gc heap.  They are considerably more lightweight than ref types.  However, unlike ref classes, they cannot be inherited from.

Okay, well that sounds simple.  There's one more wrench in the works: there's a special subset of value types called simple value types.  These are value types that don't contain any handles (^) as members.  With this restriction comes a benefit: simple value types can be allocated on the native heap.

Let's look at the code for a value class:

  value class Point{
    int x,y;
    Point(int a, int b):x(a), y(b){}
    int GetX(){ return x; }
    int GetY(){ return y; }
    void set(int a, int b){ x=a; y=b; }

This is a good example of a simple value type, because it contains no handle (^) members. 

Value types aren't too complicated, as you can plainly see.  In a future code sample, we'll look at implementing a basic data structure using completely managed code.