13.07.2015 Views

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 17 Classesaccessor-declarations:get-accessor-declaration set-accessor-declaration optset-accessor-declaration get-accessor-declaration optget-accessor-declaration:attributes opt get accessor-bodyset-accessor-declaration:attributes opt set accessor-bodyaccessor-body:block;The accessor declarations consist of a get-accessor-declaration, a set-accessor-declaration, or both. Eachaccessor declaration consists of the token get or set followed by an accessor-body. For abstract and externproperties, the accessor-body for each accessor specified is simply a semicolon. For the accessors of any nonabstract,non-extern property, the accessor-body is a block which specifies the statements to be executed whenthe corresponding accessor is invoked.A get accessor corresponds to a parameterless method with a return value of the property type. Except as thetarget of an assignment, when a property is referenced in an expression, the get accessor of the property isinvoked to compute the value of the property (§14.1.1). The body of a get accessor must conform to the rules forvalue-returning methods described in §17.5.8. In particular, all return statements in the body of a get accessormust specify an expression that is implicitly convertible to the property type. Furthermore, the endpoint of a getaccessor must not be reachable.A set accessor corresponds to a method with a single value parameter of the property type and a void returntype. The implicit parameter of a set accessor is always named value. When a property is referenced as thetarget of an assignment (§14.13), or as the operand of ++ or –- (§14.5.9, 14.6.5), the set accessor is invokedwith an argument (whose value is that of the right-hand side of the assignment or the operand of the ++ or –-operator) that provides the new value (§14.13.1). The body of a set accessor must conform to the rules for voidmethods described in §17.5.8. In particular, return statements in the set accessor body are not permitted tospecify an expression. Since a set accessor implicitly has a parameter named value, it is a compile-time errorfor a local variable declaration or a local constant declaration in a set accessor to have that name.Based on the presence or absence of the get and set accessors, a property is classified as follows:• A property that includes both a get accessor and a set accessor is said to be a read-write property.• A property that has only a get accessor is said to be a read-only property. It is a compile-time error for aread-only property to be the target of an assignment.• A property that has only a set accessor is said to be a write-only property. Except as the target of anassignment, it is a compile-time error to reference a write-only property in an expression. [Note: The pre- andpostfix ++ and -- operators cannot be applied to write-only properties, since these operators read the oldvalue of their operand before they write the new one. end note][Example: In the examplepublic class Button: Control{private string caption;241

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!