X hits on this document

917 views

0 shares

0 downloads

0 comments

245 / 396

C# LANGUAGE SPECIFICATION

accessor-body: block ;

The accessor declarations consist of a get-accessor-declaration, a set-accessor-declaration, or both. Each accessor declaration consists of the token get or set followed by an accessor-body. For abstract and extern properties, the accessor-body for each accessor specified is simply a semicolon. For the accessors of any non-abstract, non-extern property, the accessor-body is a block which specifies the statements to be executed when the corresponding accessor is invoked.

A get accessor corresponds to a parameterless method with a return value of the property type. Except as the target of an assignment, when a property is referenced in an expression, the get accessor of the property is invoked to compute the value of the property (§‎7.1.1). The body of a get accessor must conform to the rules for value-returning methods described in §‎10.5.8. In particular, all return statements in the body of a get accessor must specify an expression that is implicitly convertible to the property type. Furthermore, the endpoint of a get accessor must not be reachable.

A set accessor corresponds to a method with a single value parameter of the property type and a void return type. The implicit parameter of a set accessor is always named value. When a property is referenced as the target of an assignment (§‎7.13), or as the operand of ++ or -- (§‎7.5.9, §‎7.6.5), the set accessor is invoked with 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 (§‎7.13.1). The body of a set accessor must conform to the rules for void methods described in §‎10.5.8. In particular, return statements in the set accessor body are not permitted to specify an expression. Since a set accessor implicitly has a parameter named value, it is a compile-time error for a local variable or 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 a read-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 an assignment, it is a compile-time error to reference a write-only property in an expression.

In the example

public class Button: Control { private string caption;

public string Caption { get { return caption; } set { if (caption != value) { caption = value; Repaint(); } } }

public override void Paint(Graphics g, Rectangle r) { // Painting code goes here } }

232Copyright Microsoft Corporation 1999-2003. All Rights Reserved.

Document info
Document views917
Page views917
Page last viewedWed Dec 07 10:49:25 UTC 2016
Pages396
Paragraphs9401
Words133190

Comments