Creating Value Types

I l @ ve RuBoard

The .NET framework recognizes two distinct families of types: value types and reference types.

Value types are relatively simple, small structures that can be created and used directly on the stack. Reference types are essentially objects in their own right, are created on the managed heap, and are subject to Garbage Collected lifetime management.

A value type might be a simple storage type such as an int or a float . It could also be a small structure or class such as time_t or CPoint . A reference type is a class, an interface, an array, and so forth.

Value types created and used on the stack are not subject to GC management because they are effectively deleted by the movement of the stack pointer when they go out of scope. This implies that they are simple to create and take minimal time to manage. It might be necessary to create a structure on the stack and pass it to some other function. For this reason, the managed runtime uses the technique of boxing the simple structure in a wrapper object that holds a copy of the original data but is reassigned on the managed heap.

To prepare your own simple structures for use as value types, you can use the __value keyword:

 __value struct Birthday {     int day;     int month;     int year; }; 

Note also that the __value keyword does not imply the __gc keyword, so if you want to be able to assign your value type on the managed heap as well, you must use both modifiers where appropriate:

 __value __gc struct Birthday {     int day;     int month;     int year; }; 

NOTE

The order of modifiers is important. If you use __gc __value , you'll get an error.


Listing 1.4.7 shows a technique that would make any C++ programmer cringe with pain. It creates simple values on the local stack and passes a pointer to them for inclusion in a collection. Under unmanaged C++, this would cause a disastrous error when the collection tried to reference items that had already gone out of scope. Under managed C++, the Garbage Col lector takes over lifetime management of the values referred to and won't allow them to be deleted until everything has let go of them.

Listing 1.4.7 refdemo.cpp : Value Types Under GC Control
 #using <mscorlib.dll> using namespace System; using namespace System::Collections; __value struct simple {     int i;     float f; }; __gc class refdemo {     ArrayList* l; public:     refdemo(){ l=new ArrayList;}     void Add(Object* o)     {         l->Add(o);     }     void create(int n,Object **pO)     {         simple s;         s.i=n;         s.f=n*3.1415926;         *pO =__box(s);     }     void init()     {         for(int x=0;x<10;x++)         {             Object *pO;             create(x,&pO);             Add(pO);         }     }     void show()     {         for(int x=0; x<l->get_Count(); x++)         {             try             {                simple* pS=__try_cast<simple*>(l->get_Item(x));                Console::WriteLine("{ 0}  * PI = { 1} ",__box(pS->i), __box(pS->f));             }             catch(System::InvalidCastException *)             {                Console::WriteLine("Bad __try_cast...");             }         }     } }; void main() {     refdemo *pD=new refdemo;     pD->init(); // create the list of values     pD->show(); // iterate the list and display the contents. } 

Creating a value type with the __value keyword changes your simple structure into a class derived from System::ValueType . This means that the value type itself can override some of the methods in the base class to allow it to participate more fully in operations with managed code. One of the more common uses of a value class is to extract a string from it. This mechanism allows you to send a boxed integer or float to the Console::WriteLine parameter replacement function and get your types to print themselves out. In this way you don't have to worry about the correct printf format code for an integer, float, or string. The object knows how to return a string describing itself.

I l @ ve RuBoard


C# and the .NET Framework. The C++ Perspective
C# and the .NET Framework
ISBN: 067232153X
EAN: 2147483647
Year: 2001
Pages: 204

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net