13 Properties
This documentation is archived and is not being maintained.

13 Properties

Visual Studio .NET 2003

The Managed Extensions provide a mechanism to write and import a class that contains a common language runtime property. To client code, a property has the appearance of an ordinary data member, and can be written to or read from using the same syntax as a data member. However, instead of declaring a data member, the user declares get and set methods which implement the property, and the compiler injects a pseudo data member which corresponds to the property methods.


// __property.cpp
// compile with: /clr
#using <mscorlib.dll>
__gc struct G {
   __property int get_Size() { return 0; };
   __property void set_Size(int i) { };
   // int Size;   compiler generated pseudo member

int main() {
   G * pG = new G;
   pG->Size = 10;   // calls set_Size
   int i = pG->Size;   // calls get_Size


  • The __property keyword is used to distinguish a property method from an ordinary method.
  • The name of the get method and the set method are the same except for the prefix.
  • The get and set methods need not agree on the virtual function-specifier.
  • The accessibility of the get and set method can differ.
  • It is not necessary for a property to have both a get and a set method.
  • A property can be made pure virtual using the =0 syntax similar to an ordinary method.
  • The definition of a property method can appear outside the class body similar to an ordinary method.


Some restrictions apply for managed properties.

  • A property shall be declared using the __property keyword.
  • In a managed class, any member whose identifier follows the common language runtime property naming convention of get_ or set_ shall define either a get or a set property method.
  • The get and the set method for a property shall agree on the static storage-class-specifier.
© 2016 Microsoft