⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 17.7.htm

📁 This ECMA Standard specifies the form and establishes the interpretation of programs written in the
💻 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.3.htm"><img src="previous.gif" alt="previous" border="0" /></a><a href="17.7.1.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.6.htm"><img src="previous.gif" alt="previous at this level" border="0" /></a><a href="17.8.htm"><img src="next.gif" alt="next at this level" border="0" /></a> <span class="clause-number">17.7</span><span class="clause-title"> Events</span></span><span class="locator">
     Paragraph 1</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P1S1"></a>An event is a member that enables an object or class to provide notifications.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P1S2"></a>Clients can attach executable code for events by supplying event handlers.</span> </span><span class="locator">
     Paragraph 2</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P2S1"></a>Events are declared using event-declarations: <span class="grammar-production"><span class="name"><a name="event-declaration"></a>event-declaration</span> : <span class="rhs"><span class="non-terminal"><a href="24.2.htm#attributes">attributes</a></span><sub>opt</sub> <span class="non-terminal"><a href="17.7.htm#event-modifiers">event-modifiers</a></span><sub>opt</sub> <span class="keyword">event</span> <span class="non-terminal"><a href="11.htm#type">type</a></span> <span class="non-terminal"><a href="17.4.htm#variable-declarators">variable-declarators</a></span> <span class="terminal">;</span> </span><span class="rhs"><span class="non-terminal"><a href="24.2.htm#attributes">attributes</a></span><sub>opt</sub> <span class="non-terminal"><a href="17.7.htm#event-modifiers">event-modifiers</a></span><sub>opt</sub> <span class="keyword">event</span> <span class="non-terminal"><a href="11.htm#type">type</a></span> <span class="non-terminal"><a href="17.6.htm#member-name">member-name</a></span> <span class="terminal">{</span> <span class="non-terminal"><a href="17.7.htm#event-accessor-declarations">event-accessor-declarations</a></span> <span class="terminal">}</span> </span></span><span class="grammar-production"><span class="name"><a name="event-modifiers"></a>event-modifiers</span> : <span class="rhs"><span class="non-terminal"><a href="17.7.htm#event-modifier">event-modifier</a></span> </span><span class="rhs"><span class="non-terminal"><a href="17.7.htm#event-modifiers">event-modifiers</a></span> <span class="non-terminal"><a href="17.7.htm#event-modifier">event-modifier</a></span> </span></span><span class="grammar-production"><span class="name"><a name="event-modifier"></a>event-modifier</span> : <span class="rhs"><span class="keyword">new</span> </span><span class="rhs"><span class="keyword">public</span> </span><span class="rhs"><span class="keyword">protected</span> </span><span class="rhs"><span class="keyword">internal</span> </span><span class="rhs"><span class="keyword">private</span> </span><span class="rhs"><span class="keyword">static</span> </span><span class="rhs"><span class="keyword">virtual</span> </span><span class="rhs"><span class="keyword">sealed</span> </span><span class="rhs"><span class="keyword">override</span> </span><span class="rhs"><span class="keyword">abstract</span> </span><span class="rhs"><span class="keyword">extern</span> </span></span><span class="grammar-production"><span class="name"><a name="event-accessor-declarations"></a>event-accessor-declarations</span> : <span class="rhs"><span class="non-terminal"><a href="17.7.htm#add-accessor-declaration">add-accessor-declaration</a></span> <span class="non-terminal"><a href="17.7.htm#remove-accessor-declaration">remove-accessor-declaration</a></span> </span><span class="rhs"><span class="non-terminal"><a href="17.7.htm#remove-accessor-declaration">remove-accessor-declaration</a></span> <span class="non-terminal"><a href="17.7.htm#add-accessor-declaration">add-accessor-declaration</a></span> </span></span><span class="grammar-production"><span class="name"><a name="add-accessor-declaration"></a>add-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">add</span> <span class="non-terminal"><a href="15.2.htm#block">block</a></span> </span></span><span class="grammar-production"><span class="name"><a name="remove-accessor-declaration"></a>remove-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">remove</span> <span class="non-terminal"><a href="15.2.htm#block">block</a></span> </span></span></span></span><span class="locator">
     Paragraph 3</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P3S1"></a>An <span class="non-terminal"><a href="17.7.htm#event-declaration">event-declaration</a></span> may include a set of attributes (<a href="24.htm">&#167;24</a>) and a valid combination of the four access modifiers (<a href="17.2.3.htm">&#167;17.2.3</a>), the new (<a href="17.2.2.htm">&#167;17.2.2</a>), static (<a href="17.5.2.htm">&#167;17.5.2</a>, <a href="17.7.3.htm">&#167;17.7.3</a>), virtual (<a href="17.5.3.htm">&#167;17.5.3</a>, <a href="17.7.4.htm">&#167;17.7.4</a>), override (<a href="17.5.4.htm">&#167;17.5.4</a>, <a href="17.7.4.htm">&#167;17.7.4</a>), sealed (<a href="17.5.5.htm">&#167;17.5.5</a>), abstract (<a href="17.5.6.htm">&#167;17.5.6</a>, <a href="17.7.4.htm">&#167;17.7.4</a>), and extern modifiers.</span> </span><span class="locator">
     Paragraph 4</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P4S1"></a>Event declarations are subject to the same rules as method declarations (<a href="17.5.htm">&#167;17.5</a>) with regard to valid combinations of modifiers.</span> </span><span class="locator">
     Paragraph 5</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P5S1"></a>The type of an event declaration must be a <span class="non-terminal"><a href="11.2.htm#delegate-type">delegate-type</a></span> (<a href="11.2.htm">&#167;11.2</a>), and that <span class="non-terminal"><a href="11.2.htm#delegate-type">delegate-type</a></span> must be at least as accessible as the event itself (<a href="10.5.4.htm">&#167;10.5.4</a>).</span> </span><span class="locator">
     Paragraph 6</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P6S1"></a>An event declaration may include <span class="non-terminal"><a href="17.7.htm#event-accessor-declarations">event-accessor-declarations</a></span>.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P6S2"></a>However, if it does not, for non-extern,  non-abstract events, the compiler shall supply them automatically (<a href="17.7.1.htm">&#167;17.7.1</a>); for extern events, the accessors are provided externally.</span> </span><span class="locator">
     Paragraph 7</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P7S1"></a>An event declaration that omits <span class="non-terminal"><a href="17.7.htm#event-accessor-declarations">event-accessor-declarations</a></span> defines one or more events-one for each of the <span class="non-terminal"><a href="17.4.htm#variable-declarator">variable-declarator</a></span>s.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P7S2"></a>The attributes and modifiers apply to all of the members declared by such an  <span class="non-terminal"><a href="17.7.htm#event-declaration">event-declaration</a></span>.</span> </span><span class="locator">
     Paragraph 8</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P8S1"></a>It is a compile-time error for an <span class="non-terminal"><a href="17.7.htm#event-declaration">event-declaration</a></span> to include both the abstract modifier and  brace-delimited <span class="non-terminal"><a href="17.7.htm#event-accessor-declarations">event-accessor-declarations</a></span>.</span> </span><span class="locator">
     Paragraph 9</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P9S1"></a>When an event declaration includes an extern modifier, the event is said to be an external event.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P9S2"></a>Because an external event declaration provides no actual implementation, it is an error for it to include both the extern modifier and <span class="non-terminal"><a href="17.7.htm#event-accessor-declarations">event-accessor-declarations</a></span>.</span> </span><span class="locator">
     Paragraph 10</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P10S1"></a>An event can be used as the left-hand operand of the += and -= operators (<a href="14.13.3.htm">&#167;14.13.3</a>).</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P10S2"></a>These operators are used, respectively, to attach event handlers to, or to remove event handlers from an event, and the access modifiers of the event control the contexts in which such operations are permitted.</span> </span><span class="locator">
     Paragraph 11</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P11S1"></a>Since += and -= are the only operations that are permitted on an event outside the type that declares the event, external code can add and remove handlers for an event, but cannot in any other way obtain or modify the underlying list of event handlers.</span> </span><span class="locator">
     Paragraph 12</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P12S1"></a>In an operation of the form x += y or x -= y, when x is an event and the reference takes place outside the type that contains the declaration of x, the result of the operation has type <span class="keyword">void</span> (as opposed to having the type of x, with the value of x after the assignment).</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P12S2"></a>This rule prohibits external code from indirectly examining the underlying delegate of an event.</span> </span><span class="paragraph"><span class="example">[Example: The following example shows how event handlers are attached to instances of the Button class: <pre class="code-example">
public delegate void EventHandler(object sender, EventArgs e);  
public class Button: Control  
{  
   public event EventHandler Click;  
}  
public class LoginDialog: Form  
{  
   Button OkButton;  
   Button CancelButton;  
   public LoginDialog() {  
      OkButton = new Button(...);  
      OkButton.Click += new EventHandler(OkButtonClick);  
      CancelButton = new Button(...);  
      CancelButton.Click += new EventHandler(CancelButtonClick);  
   }  
   void OkButtonClick(object sender, EventArgs e) {  
      // Handle OkButton.Click event  
   }  
   void CancelButtonClick(object sender, EventArgs e) {  
      // Handle CancelButton.Click event  
   }  
}  
</pre></span></span><span class="paragraph"><span class="example">Here, the LoginDialog instance constructor creates two Button instances and attaches event handlers to the Click events. end example]</span> </span><span class="ruler"></span><table><tr><td><table align="left" bgcolor="navy"><tr bgcolor="navy"><td><font face="Arial,sans-serif" size="6" color="yellow"><strong>{ JSL }</strong></font></td></tr></table></td></tr><tr><td><font face="Arial,sans-serif" size="2" color="navy"><strong>Jagger Software Ltd</strong></font></td></tr><tr><td><font face="Arial,sans-serif" size="2" color="navy"><strong>Company # 4070126</strong></font></td></tr><tr><td><font face="Arial,sans-serif" size="2" color="navy"><strong>VAT # 762 5213 42</strong></font></td></tr></table><img src="valid-html401.png" align="left" height="31" width="88" alt="Valid HTML 4.01" /><img src="vcss.gif" align="left" height="31" width="88" alt="Valid CSS" /></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -