共用方式為


property 

Declare a managed property.

modifier property type property_name;   // property data member
modifier property type property_name {   // property block
      modifier void set(type);
      modifier type get();
}
modifier property type property_name[,] {
      modifier void set(type);
      modifier type get();
}

Parameters

  • [,]
    The notation for an indexed property. Commas are optional; for each additional parameter you want the accessor methods to take, add a comma.
  • modifier
    A modifier that can be used on either the event declaration or on an event accessor method. Possible values are static and virtual.
  • property_name
    Parameter(s) for the raise method, must match the signature of the delegate.
  • type
    The type of the value represented by the property.

Remarks

The property keyword introduces the declaration of a property and can appear in a class, interface, or value type. A property can have a getter function (read only), a setter function (write only), or both (read-write).

A property name cannot match the name of the managed class containing it. The return type of the getter function must match the type of the last parameter of a corresponding setter function.

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.

The get and set methods need not agree on the virtual modifier.

The accessibility of the get and set method can differ.

The definition of a property method can appear outside the class body similar to an ordinary method.

The get and the set method for a property shall agree on the static modifier.

A property is scalar if its get and set methods fit the following description:

  • The get method has no parameters, and has return type T.

  • The set method has a single parameter of type T, and void return type.

There shall be only one scalar property declared in a single scope with the same identifier. Scalar properties cannot be overloaded.

When a property data member is declared, the compiler will inject a data member, sometimes called the "backing store", in the class. However, the name of the data member will be of a form, such that, you cannot reference the member in the source as if it were an actual data member of the containing class. Use ildasm.exe to view the metadata for your type and see the compiler generated name for the property's backing store.

Different accessibility is allowed for the accessor methods in a property block. That is, the set method can be public and the get method can be private. However, it is an error for an accessor method to have a less restrictive accessibility than what is on the declaration of the property itself.

property is a context-sensitive keyword. See Context-Sensitive Keywords for more information.

For more information about properties, see

The following example shows the declaration and use of a property data member and a property block. It also shows that a property accessor can be defined out of class.

Example

// mcppv2_property.cpp
// compile with: /clr
using namespace System;
public ref class C {
   int MyInt;
public:

   // property data member
   property String ^ Simple_Property;

   // property block
   property int Property_Block {

      int get();

      void set(int value) {
         MyInt = value;
      }
   }
};

int C::Property_Block::get() {
   return MyInt;
}

int main() {
   C ^ MyC = gcnew C();
   MyC->Simple_Property = "test";
   Console::WriteLine(MyC->Simple_Property);

   MyC->Property_Block = 21;
   Console::WriteLine(MyC->Property_Block);
}

Output

test
21

See Also

Concepts

New C++ Language Features