📄 17.6.2.htm
字号:
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Hyperlinked ECMA C# Language Specification</title><meta name="author" content="Jon Jagger" /><link rel="stylesheet" href="ecma334.css"></link></head><body><div align="right"><em><a href="http://www.jaggersoft.com">Jon Jagger</a></em></div><div align="right"><a href="mailto:jon@jaggersoft.com">jon@jaggersoft.com</a></div><form method="get" action="http://search.atomz.com/search/"><input size="30" name="sp-q"></input><input type="submit" value="Search C# Spec"></input><input type="hidden" name="sp-a" value="sp10024177"></input><input type="hidden" name="sp-f" value="ISO-8859-1"></input></form><a href="toc.htm">Table of Contents</a> <a href="1.htm">1</a> <a href="2.htm">2</a> <a href="3.htm">3</a> <a href="4.htm">4</a> <a href="5.htm">5</a> <a href="6.htm">6</a> <a href="7.htm">7</a> <a href="8.htm">8</a> <a href="9.htm">9</a> <a href="10.htm">10</a> <a href="11.htm">11</a> <a href="12.htm">12</a> <a href="13.htm">13</a> <a href="14.htm">14</a> <a href="15.htm">15</a> <a href="16.htm">16</a> <a href="17.htm">17</a> <a href="18.htm">18</a> <a href="19.htm">19</a> <a href="20.htm">20</a> <a href="21.htm">21</a> <a href="22.htm">22</a> <a href="23.htm">23</a> <a href="24.htm">24</a> <a href="25.htm">25</a> <a href="notes.htm">Notes</a> <a href="HyperlinkedCSharpECMA.zip">Download</a><span class="ruler"></span><span class="heading">ECMA-334 C# Language Specification</span><span class="navigate"><a href="17.6.1.htm"><img src="previous.gif" alt="previous" border="0" /></a><a href="17.6.3.htm"><img src="next.gif" alt="next" border="0" /></a></span><span class="clause-depth"><a href="16.htm"><img src="previous.gif" alt="previous at this level" border="0" /></a><a href="18.htm"><img src="next.gif" alt="next at this level" border="0" /></a> <span class="clause-number-link"><a href="17.htm">17</a></span><span class="clause-title-previous"> Classes</span></span><span class="clause-depth"><a href="17.5.htm"><img src="previous.gif" alt="previous at this level" border="0" /></a><a href="17.7.htm"><img src="next.gif" alt="next at this level" border="0" /></a> <span class="clause-number-link"><a href="17.6.htm">17.6</a></span><span class="clause-title-previous"> Properties</span></span><span class="clause-depth"><a href="17.6.1.htm"><img src="previous.gif" alt="previous at this level" border="0" /></a><a href="17.6.3.htm"><img src="next.gif" alt="next at this level" border="0" /></a> <span class="clause-number">17.6.2</span><span class="clause-title"> Accessors</span></span><span class="locator">
Paragraph 1</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P1S1"></a>The <span class="non-terminal"><a href="17.6.2.htm#accessor-declarations">accessor-declarations</a></span> of a property specify the executable statements associated with reading and writing that property.</span> <span class="grammar-production"><span class="name"><a name="accessor-declarations"></a>accessor-declarations</span> : <span class="rhs"><span class="non-terminal"><a href="17.6.2.htm#get-accessor-declaration">get-accessor-declaration</a></span> <span class="non-terminal"><a href="17.6.2.htm#set-accessor-declaration">set-accessor-declaration</a></span><sub>opt</sub> </span><span class="rhs"><span class="non-terminal"><a href="17.6.2.htm#set-accessor-declaration">set-accessor-declaration</a></span> <span class="non-terminal"><a href="17.6.2.htm#get-accessor-declaration">get-accessor-declaration</a></span><sub>opt</sub> </span></span><span class="grammar-production"><span class="name"><a name="get-accessor-declaration"></a>get-accessor-declaration</span> : <span class="rhs"><span class="non-terminal"><a href="24.2.htm#attributes">attributes</a></span><sub>opt</sub> <span class="terminal">get</span> <span class="non-terminal"><a href="17.6.2.htm#accessor-body">accessor-body</a></span> </span></span><span class="grammar-production"><span class="name"><a name="set-accessor-declaration"></a>set-accessor-declaration</span> : <span class="rhs"><span class="non-terminal"><a href="24.2.htm#attributes">attributes</a></span><sub>opt</sub> <span class="terminal">set</span> <span class="non-terminal"><a href="17.6.2.htm#accessor-body">accessor-body</a></span> </span></span><span class="grammar-production"><span class="name"><a name="accessor-body"></a>accessor-body</span> : <span class="rhs"><span class="non-terminal"><a href="15.2.htm#block">block</a></span> </span><span class="rhs"><span class="terminal">;</span> </span></span></span><span class="locator">
Paragraph 2</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P2S1"></a>The accessor declarations consist of a <span class="non-terminal"><a href="17.6.2.htm#get-accessor-declaration">get-accessor-declaration</a></span>, a <span class="non-terminal"><a href="17.6.2.htm#set-accessor-declaration">set-accessor-declaration</a></span>, or both.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P2S2"></a>Each accessor declaration consists of the token get or set followed by an <span class="non-terminal"><a href="17.6.2.htm#accessor-body">accessor-body</a></span>.</span> <span class="sentence"><span class="sentence-number">3</span> <a name="P2S3"></a>For abstract and extern properties, the <span class="non-terminal"><a href="17.6.2.htm#accessor-body">accessor-body</a></span> for each accessor specified is simply a semicolon.</span> <span class="sentence"><span class="sentence-number">4</span> <a name="P2S4"></a>For the accessors of any non-abstract, non-extern property, the <span class="non-terminal"><a href="17.6.2.htm#accessor-body">accessor-body</a></span> is a block which specifies the statements to be executed when the corresponding accessor is invoked.</span> </span><span class="locator">
Paragraph 3</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P3S1"></a>A get accessor corresponds to a parameterless method with a return value of the property type.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P3S2"></a>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 (<a href="14.1.1.htm">§14.1.1</a>).</span> <span class="sentence"><span class="sentence-number">3</span> <a name="P3S3"></a>The body of a get accessor must conform to the rules for value-returning methods described in <a href="17.5.8.htm">§17.5.8</a>.</span> <span class="sentence"><span class="sentence-number">4</span> <a name="P3S4"></a>In particular, all return statements in the body of a get accessor must specify an expression that is implicitly convertible to the property type.</span> <span class="sentence"><span class="sentence-number">5</span> <a name="P3S5"></a>Furthermore, the endpoint of a get accessor must not be reachable.</span> </span><span class="locator">
Paragraph 4</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P4S1"></a>A set accessor corresponds to a method with a single value parameter of the property type and a <span class="keyword">void</span> return type.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P4S2"></a>The implicit parameter of a set accessor is always named value.</span> <span class="sentence"><span class="sentence-number">3</span> <a name="P4S3"></a>When a property is referenced as the target of an assignment (<a href="14.13.htm">§14.13</a>), or as the operand of ++ or (<a href="14.5.9.htm">§14.5.9</a>, --14.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 (<a href="14.13.1.htm">§14.13.1</a>).</span> <span class="sentence"><span class="sentence-number">4</span> <a name="P4S4"></a>The body of a set accessor must conform to the rules for <span class="keyword">void</span> methods described in <a href="17.5.8.htm">§17.5.8</a>.</span> <span class="sentence"><span class="sentence-number">5</span> <a name="P4S5"></a>In particular, return statements in the set accessor body are not permitted to specify an expression.</span> <span class="sentence"><span class="sentence-number">6</span> <a name="P4S6"></a>Since a set accessor implicitly has a parameter named value, it is a compile-time error for a local variable declaration in a set accessor to have that name.</span> </span><span class="locator">
Paragraph 5</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P5S1"></a>Based on the presence or absence of the get and set accessors, a property is classified as follows: </span><ul><li><span class="sentence"><span class="sentence-number">2</span> <a name="P5S2"></a> A property that includes both a get accessor and a set accessor is said to be a read-write property.</span> </li><li><span class="sentence"><span class="sentence-number">3</span> <a name="P5S3"></a> A property that has only a get accessor is said to be a read-only property.</span> <span class="sentence"><span class="sentence-number">4</span> <a name="P5S4"></a>It is a compile-time error for a read-only property to be the target of an assignment.</span> </li><li><span class="sentence"><span class="sentence-number">5</span> <a name="P5S5"></a> A property that has only a set accessor is said to be a write-only property.</span> <span class="sentence"><span class="sentence-number">6</span> <a name="P5S6"></a>Except as the target of an assignment, it is a compile-time error to reference a write-only property in an expression.</span> <span class="note">[Note: The pre-and postfix ++ and --operators cannot be applied to write-only properties, since these operators read the old value of their operand before they write the new one. end note]</span> </li></ul></span><span class="paragraph"><span class="example">[Example: In the example <pre class="code-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
}
}
</pre>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 current value, and if so, it stores the new value and repaints the control. Properties often follow the pattern shown above: The get accessor simply returns a value stored in a private field, and the set accessor modifies that private field and then performs any additional actions required to fully update the state of the object. </span></span><span class="paragraph"><span class="example">Given the Button class above, the following is an example of use of the Caption property: <pre class="code-example">
Button okButton = new Button();
okButton.Caption = "OK"; // Invokes set accessor
string s = okButton.Caption; // Invokes get accessor
</pre></span></span><span class="paragraph"><span class="example">Here, the set accessor is invoked by assigning a value to the property, and the get accessor is invoked by referencing the property in an expression. end example]</span> </span><span class="locator">
Paragraph 6</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P6S1"></a>The get and set accessors of a property are not distinct members, and it is not possible to declare the accessors of a property separately.</span> <span class="note">[Note: As such, it is not possible for the two accessors of a read-write property to have different accessibility. end note]</span> <span class="example">[Example: The example <pre class="code-example">
class A
{
private string name;
public string Name { // Error, duplicate member name
get { return name; }
}
public string Name { // Error, duplicate member name
set { name = value; }
}
}
</pre>does not declare a single read-write property. Rather, it declares two properties with the same name, one read-only and one write-only. Since two members declared in the same class cannot have the same name, the example causes a compile-time error to occur. end example]</span> </span><span class="locator">
Paragraph 7</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P7S1"></a>When a derived class declares a property by the same name as an inherited property, the derived property hides the inherited property with respect to both reading and writing.</span> <span class="example">[Example: In the example <pre class="code-example">
class A
{
public int P {
set {...}
}
}
class B: A
{
new public int P {
get {...}
}
}
</pre>the P property in B hides the P property in A with respect to both reading and writing. Thus, in the statements <pre class="code-example">
B b = new B();
b.P = 1; // Error, B.P is read-only
((A)b).P = 1; // Ok, reference to A.P
</pre>the assignment to b.P causes a compile-time error to be reported, since the read-only P property in B hides the write-only P property in A. Note, however, that a cast can be used to access the hidden P property. end example]</span> </span><span class="locator">
Paragraph 8</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P8S1"></a>Unlike public fields, properties provide a separation between an object's internal state and its public interface.</span> <span class="example">[Example: Consider the example: <pre class="code-example">
class Label
{
private int x, y;
private string caption;
public Label(int x, int y, string caption) {
this.x = x;
this.y = y;
this.caption = caption;
}
public int X {
get { return x; }
}
public int Y {
get { return y; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -