📄 14.5.10.3.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="14.5.10.2.htm"><img src="previous.gif" alt="previous" border="0" /></a><a href="14.5.11.htm"><img src="next.gif" alt="next" border="0" /></a></span><span class="clause-depth"><a href="13.htm"><img src="previous.gif" alt="previous at this level" border="0" /></a><a href="15.htm"><img src="next.gif" alt="next at this level" border="0" /></a> <span class="clause-number-link"><a href="14.htm">14</a></span><span class="clause-title-previous"> Expressions</span></span><span class="clause-depth"><a href="14.4.htm"><img src="previous.gif" alt="previous at this level" border="0" /></a><a href="14.6.htm"><img src="next.gif" alt="next at this level" border="0" /></a> <span class="clause-number-link"><a href="14.5.htm">14.5</a></span><span class="clause-title-previous"> Primary expressions</span></span><span class="clause-depth"><a href="14.5.9.htm"><img src="previous.gif" alt="previous at this level" border="0" /></a><a href="14.5.11.htm"><img src="next.gif" alt="next at this level" border="0" /></a> <span class="clause-number-link"><a href="14.5.10.htm">14.5.10</a></span><span class="clause-title-previous"> The new operator</span></span><span class="clause-depth"><a href="14.5.10.2.htm"><img src="previous.gif" alt="previous at this level" border="0" /></a><a href="14.5.10.3.htm"><img src="next.gif" alt="next at this level" border="0" /></a> <span class="clause-number">14.5.10.3</span><span class="clause-title"> Delegate creation expressions</span></span><span class="locator">
Paragraph 1</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P1S1"></a>A <span class="non-terminal"><a href="14.5.10.3.htm#delegate-creation-expression">delegate-creation-expression</a></span> is used to create a new instance of a <span class="non-terminal"><a href="11.2.htm#delegate-type">delegate-type</a></span>.</span> <span class="grammar-production"><span class="name"><a name="delegate-creation-expression"></a>delegate-creation-expression</span> : <span class="rhs"><span class="keyword">new</span> <span class="non-terminal"><a href="11.2.htm#delegate-type">delegate-type</a></span> <span class="terminal">(</span> <span class="non-terminal"><a href="14.14.htm#expression">expression</a></span> <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 argument of a delegate creation expression must be a method group (<a href="14.1.htm">§14.1</a>) or a value of a <span class="non-terminal"><a href="11.2.htm#delegate-type">delegate-type</a></span>.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P2S2"></a>If the argument is a method group, it identifies the method and, for an instance method, the object for which to create a delegate.</span> <span class="sentence"><span class="sentence-number">3</span> <a name="P2S3"></a>If the argument is a value of a <span class="non-terminal"><a href="11.2.htm#delegate-type">delegate-type</a></span>, it identifies a delegate instance of which to create a copy.</span> </span><span class="locator">
Paragraph 3</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P3S1"></a>The compile-time processing of a <span class="non-terminal"><a href="14.5.10.3.htm#delegate-creation-expression">delegate-creation-expression</a></span> of the form new D(E), where D is a <span class="non-terminal"><a href="11.2.htm#delegate-type">delegate-type</a></span> and E is an expression, consists of the following steps: </span><ul><li><span class="sentence"><span class="sentence-number">2</span> <a name="P3S2"></a> If E is a method group: </span></li><ul><li><span class="sentence"><span class="sentence-number">3</span> <a name="P3S3"></a> The set of methods identified by E must include exactly one method that is compatible (<a href="22.1.htm">§22.1</a>) with D, and this method becomes the one to which the newly created delegate refers.</span> <span class="sentence"><span class="sentence-number">4</span> <a name="P3S4"></a>If no matching method exists, or if more than one matching method exists, a compile-time error occurs.</span> <span class="sentence"><span class="sentence-number">5</span> <a name="P3S5"></a>If the selected method is an instance method, the instance expression associated with E determines the target object of the delegate.</span> </li><li><span class="sentence"><span class="sentence-number">6</span> <a name="P3S6"></a> As in a method invocation, the selected method must be compatible with the context of the method group: If the method is a static method, the method group must have resulted from a <span class="non-terminal"><a href="14.5.2.htm#simple-name">simple-name</a></span> or a <span class="non-terminal"><a href="14.5.4.htm#member-access">member-access</a></span> through a type.</span> <span class="sentence"><span class="sentence-number">7</span> <a name="P3S7"></a>If the method is an instance method, the method group must have resulted from a <span class="non-terminal"><a href="14.5.2.htm#simple-name">simple-name</a></span> or a <span class="non-terminal"><a href="14.5.4.htm#member-access">member-access</a></span> through a variable or value.</span> <span class="sentence"><span class="sentence-number">8</span> <a name="P3S8"></a>If the selected method does not match the context of the method group, a compile-time error occurs.</span> </li><li><span class="sentence"><span class="sentence-number">9</span> <a name="P3S9"></a> The result is a value of type D, namely a newly created delegate that refers to the selected method and target object.</span> </li></ul><li><span class="sentence"><span class="sentence-number">10</span> <a name="P3S10"></a> Otherwise, if E is a value of a delegate-type: </span></li><ul><li><span class="sentence"><span class="sentence-number">11</span> <a name="P3S11"></a> D and E must be compatible (<a href="22.1.htm">§22.1</a>); otherwise, a compile-time error occurs.</span> </li><li><span class="sentence"><span class="sentence-number">12</span> <a name="P3S12"></a> The result is a value of type D, namely a newly created delegate that refers to the same invocation list as E.</span> </li></ul><li><span class="sentence"><span class="sentence-number">13</span> <a name="P3S13"></a> Otherwise, the delegate creation expression is invalid, and a compile-time error occurs.</span> </li></ul></span><span class="locator">
Paragraph 4</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P4S1"></a>The run-time processing of a <span class="non-terminal"><a href="14.5.10.3.htm#delegate-creation-expression">delegate-creation-expression</a></span> of the form new D(E), where D is a <span class="non-terminal"><a href="11.2.htm#delegate-type">delegate-type</a></span> and E is an expression, consists of the following steps: </span><ul><li><span class="sentence"><span class="sentence-number">2</span> <a name="P4S2"></a> If E is a method group: </span></li><ul><li><span class="sentence"><span class="sentence-number">3</span> <a name="P4S3"></a> If the method selected at compile-time is a static method, the target object of the delegate is null.</span> <span class="sentence"><span class="sentence-number">4</span> <a name="P4S4"></a>Otherwise, the selected method is an instance method, and the target object of the delegate is determined from the instance expression associated with E: </span></li><ul><li><span class="sentence"><span class="sentence-number">5</span> <a name="P4S5"></a> The instance expression is evaluated.</span> <span class="sentence"><span class="sentence-number">6</span> <a name="P4S6"></a>If this evaluation causes an exception, no further steps are executed.</span> </li><li><span class="sentence"><span class="sentence-number">7</span> <a name="P4S7"></a> If the instance expression is of a <span class="non-terminal"><a href="11.2.htm#reference-type">reference-type</a></span>, the value computed by the instance expression becomes the target object.</span> <span class="sentence"><span class="sentence-number">8</span> <a name="P4S8"></a>If the target object is null, a System.NullReferenceException is thrown and no further steps are executed.</span> </li><li><span class="sentence"><span class="sentence-number">9</span> <a name="P4S9"></a> If the instance expression is of a <span class="non-terminal"><a href="11.1.htm#value-type">value-type</a></span>, a boxing operation (<a href="11.3.1.htm">§11.3.1</a>) is performed to convert the value to an object, and this object becomes the target object.</span> </li></ul><li><span class="sentence"><span class="sentence-number">10</span> <a name="P4S10"></a> A new instance of the delegate type D is allocated.</span> <span class="sentence"><span class="sentence-number">11</span> <a name="P4S11"></a>If there is not enough memory available to allocate the new instance, a System.OutOfMemoryException is thrown and no further steps are executed.</span> </li><li><span class="sentence"><span class="sentence-number">12</span> <a name="P4S12"></a> The new delegate instance is initialized with a reference to the method that was determined at compile-time and a reference to the target object computed above.</span> </li></ul><li><span class="sentence"><span class="sentence-number">13</span> <a name="P4S13"></a> If E is a value of a delegate-type: </span></li><ul><li><span class="sentence"><span class="sentence-number">14</span> <a name="P4S14"></a> E is evaluated.</span> <span class="sentence"><span class="sentence-number">15</span> <a name="P4S15"></a>If this evaluation causes an exception, no further steps are executed.</span> </li><li><span class="sentence"><span class="sentence-number">16</span> <a name="P4S16"></a> If the value of E is null, a System.NullReferenceException is thrown and no further steps are executed.</span> </li><li><span class="sentence"><span class="sentence-number">17</span> <a name="P4S17"></a> A new instance of the delegate type D is allocated.</span> <span class="sentence"><span class="sentence-number">18</span> <a name="P4S18"></a>If there is not enough memory available to allocate the new instance, a System.OutOfMemoryException is thrown and no further steps are executed.</span> </li><li><span class="sentence"><span class="sentence-number">19</span> <a name="P4S19"></a> The new delegate instance is initialized with references to the same invocation list as the delegate instance given by E.</span> </li></ul></ul></span><span class="locator">
Paragraph 5</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P5S1"></a>The method and object to which a delegate refers are determined when the delegate is instantiated and then remain constant for the entire lifetime of the delegate.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P5S2"></a>In other words, it is not possible to change the target method or object of a delegate once it has been created.</span> <span class="note">[Note: Remember, when two delegates are combined or one is removed from another, a new delegate results; no existing delegate has its content changed. end note]</span> </span><span class="locator">
Paragraph 6</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P6S1"></a>It is not possible to create a delegate that refers to a property, indexer, user-defined operator, instance constructor, destructor, or static constructor.</span> </span><span class="paragraph"><span class="example">[Example: As described above, when a delegate is created from a method group, the formal parameter list and return type of the delegate determine which of the overloaded methods to select. In the example <pre class="code-example">
delegate double DoubleFunc(double x);
class A
{
DoubleFunc f = new DoubleFunc(Square);
static float Square(float x) {
return x * x;
}
static double Square(double x) {
return x * x;
}
}
</pre>the A.f field is initialized with a delegate that refers to the second Square method because that method exactly matches the formal parameter list and return type of DoubleFunc. Had the second Square method not been present, a compile-time error would have occurred. 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 + -