s
[ Pobierz całość w formacie PDF ]
furthermore permit such attributes to be computed.
Properties are declared using property-declarations:
property-declaration:
attributesopt property-modifiersopt type member-name { accessor-declarations }
property-modifiers:
property-modifier
property-modifiers property-modifier
property-modifier:
new
public
protected
internal
private
static
member-name:
identifier
interface-type . identifier
A property-declaration may include set of attributes (§17), a new modifier (§10.2.2), a valid combination of the
four access modifiers (§10.2.3), and a static modifier (§10.2.5).
The type of a property declaration specifies the type of the property introduced by the declaration, and the
member-name specifies the name of the property. Unless the property is an explicit interface member
implementation, the member-name is simply an identifier. For an explicit interface member implementation
(§13.4.1) , the member-name consists of an interface-type followed by a . and an identifier.
The type of a property must be at least as accessible as the property itself (§3.3.4).
The accessor-declarations, which must be enclosed in { and } tokens, declare the accessors (§10.6.2) of the
property. The accessors specify the executable statements associated with reading and writing the property.
Even though the syntax for accessing a property is the same as that for a field, a property is not classified as a
variable. Thus, it is not possible to pass a property as a ref or out parameter.
10.6.1 Static properties
When a property declaration includes a static modifier, the property is said to be a static property. When no
static modifier is present, the property is said to be an instance property.
A static property is not associated with a specific instance, and it is an error to refer to this in the accessors of a
static property. It is furthermore an error to include a virtual, abstract, or override modifier on an
accessor of a static property.
An instance property is associated with a given instance of a class, and this instance can be accessed as this
(§7.5.7) in the accessors of the property.
When a property is referenced in a member-access (§7.5.4) of the form E.M, if M is a static property, E must
denote a type, and if M is an instance property, E must denote an instance.
The differences between static and instance members are further discussed in §10.2.5.
186 Copyright Microsoft Corporation 1999-2000. All Rights Reserved.
Chapter 10 Classes
10.6.2 Accessors
The accessor-declarations of a property specify the executable statements associated with reading and writing
the property.
accessor-declarations:
get-accessor-declaration set-accessor-declarationopt
set-accessor-declaration get-accessor-declarationopt
get-accessor-declaration:
accessor-modifieropt get accessor-body
set-accessor-declaration:
accessor-modifieropt set accessor-body
accessor-modifier:
virtual
override
abstract
accessor-body:
block
;
The accessor declarations consist of a get-accessor-declaration, a set-accessor-declaration, or both. Each
accessor declaration consists of an optional accessor-modifier, followed by the token get or set, followed by
an accessor-body. For abstract accessors, the accessor-body is simply a semicolon. For all other accessors,
the accessor-body is a block which specifies the statements to execute when the 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.7. 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, a get accessor is
required to terminate in a return statement or a throw statement, and control is not permitted to flow off the
end of the get accessor s body.
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, the set accessor is invoked with an argument 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.7. 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 an error for a local variable declaration in a
set accessor to use 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 read-only property. It is an 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 write-only property. Except as the target of an
assignment, it is an error to reference a write-only property in an expression.
Implementation note
Copyright Microsoft Corporation 1999-2000. All Rights Reserved. 187
C# LANGUAGE REFERENCE
In the .NET runtime, when a class declares a property X of type T, it is an error for the same class to also declare a method
with one of the following signatures:
T get_X();
void set_X(T value);
The .NET runtime reserves these signatures for compatibility with programming languages that do not support properties.
Note that this restriction does not imply that a C# program can use method syntax to access properties or property syntax
to access methods. It merely means that properties and methods that follow this pattern are mutually exclusive within the
same class.
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
}
}
the Button control declares a public Caption property. The get accessor of the Caption property returns the
string stored in the private caption field. The set accessor checks if the new value is different from the
[ Pobierz całość w formacie PDF ]