The C Extension Keywords

I l @ ve RuBoard

The C++ Extension Keywords

__abstract indicates that a class is an abstract base class and requires implementation of some or all of its declared methods .

__box makes a managed object from a value type like a structure or variable. The boxed object is reallocated on the Garbage Collected heap and then subject to GC lifetime management.

__delegate is the .NET answer to the callback. It is a declaration for a function signature that provides information about function return types and parameters. The delegate declaration allows both static members and instance members of a class to be called. It is used to map an event handler to a specific stimulus. There are two distinct types of delegate: Single Cast and Multicast . These allow either a simple event handler or a list of handlers for a single stimulus to be called.

__gc makes your classes or structures manageable by the Garbage Collected runtime.

__identifier allows you to use keywords from C++ as identifiers, such as class or for . The documentation from Microsoft says that its use is "strongly discouraged as a matter of style."

__nogc declares that instances of structures or classes should not be managed by the GC runtime.

__pin is used to prohibit the Garbage Collector's compacting mechanism from moving the managed class or value type. Once an instance of an object is pinned, it might be referred to reliably using a pointer. An unpinned object is likely to be moved "out from under your feet" by the actions of the Garbage Collector, which moves objects around as it cleans up the heap. This can have disastrous results.

__property Classes under the C# language can be given properties. These look like data members to external objects and are set or read by accessor functions called get_ and set_ . In reality, the property could perform some calculation or access some other function, using data supplied to the set accessor or before returning data via the get accessor.

__sealed has two uses. It can be used as a class modifier to signify that the class may not be derived from. It also might be used as a modifier on a final implementation of a virtual method to prevent it from being overridden again.

__try_cast allows you to try to cast a class to a particular type but will throw an exception at runtime if the cast fails. The syntax for __try_cast is

 __try_cast<type_id>(  expression  ) 

If the cast is unsuccessful , System::InvalidCastException is thrown. __try_cast should be used during the construction and test phases of program creation and then replaced by static_cast or dynamic_cast as required.

__typeof returns the System::Type of a declared object. Similar to [object]->GetType() , which requires an instance of an object, the __typeof keyword can return the type of an abstract declaration; for example

 MyObjct o=new MyObject; Type *pt = o->GetType(); // requires an instance of a MyObject Type *pt2 = __typeof(MyObject); // does not require an instance of MyObject 

__value Structures and simple classes might be turned into managed value types by the application of this modifier. A value type can be allocated on the GC heap and can be reclaimed by the GC runtime when it is no longer needed. Remember that value types are best kept simple because there is some overhead when they are used.

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