📄 17.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="17.2.7.4.htm"><img src="previous.gif" alt="previous" border="0" /></a><a href="17.4.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.2.htm"><img src="previous.gif" alt="previous at this level" border="0" /></a><a href="17.4.htm"><img src="next.gif" alt="next at this level" border="0" /></a> <span class="clause-number">17.3</span><span class="clause-title"> Constants</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 constant is a class member that represents a constant value: a value that can be computed at compile-time.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P1S2"></a>A <span class="non-terminal"><a href="17.3.htm#constant-declaration">constant-declaration</a></span> introduces one or more constants of a given type.</span> <span class="grammar-production"><span class="name"><a name="constant-declaration"></a>constant-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.3.htm#constant-modifiers">constant-modifiers</a></span><sub>opt</sub> <span class="keyword">const</span> <span class="non-terminal"><a href="11.htm#type">type</a></span> <span class="non-terminal"><a href="17.3.htm#constant-declarators">constant-declarators</a></span> <span class="terminal">;</span> </span></span><span class="grammar-production"><span class="name"><a name="constant-modifiers"></a>constant-modifiers</span> : <span class="rhs"><span class="non-terminal"><a href="17.3.htm#constant-modifier">constant-modifier</a></span> </span><span class="rhs"><span class="non-terminal"><a href="17.3.htm#constant-modifiers">constant-modifiers</a></span> <span class="non-terminal"><a href="17.3.htm#constant-modifier">constant-modifier</a></span> </span></span><span class="grammar-production"><span class="name"><a name="constant-modifier"></a>constant-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><span class="grammar-production"><span class="name"><a name="constant-declarators"></a>constant-declarators</span> : <span class="rhs"><span class="non-terminal"><a href="17.3.htm#constant-declarator">constant-declarator</a></span> </span><span class="rhs"><span class="non-terminal"><a href="17.3.htm#constant-declarators">constant-declarators</a></span> <span class="terminal">,</span> <span class="non-terminal"><a href="17.3.htm#constant-declarator">constant-declarator</a></span> </span></span><span class="grammar-production"><span class="name"><a name="constant-declarator"></a>constant-declarator</span> : <span class="rhs"><span class="non-terminal"><a href="9.4.2.htm#identifier">identifier</a></span> <span class="terminal">=</span> <span class="non-terminal"><a href="14.15.htm#constant-expression">constant-expression</a></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>A <span class="non-terminal"><a href="17.3.htm#constant-declaration">constant-declaration</a></span> may include a set of attributes (<a href="24.htm">§24</a>), a new modifier (<a href="17.2.2.htm">§17.2.2</a>), and a valid combination of the four access modifiers (<a href="17.2.3.htm">§17.2.3</a>).</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P2S2"></a>The attributes and modifiers apply to all of the members declared by the <span class="non-terminal"><a href="17.3.htm#constant-declaration">constant-declaration</a></span>.</span> <span class="sentence"><span class="sentence-number">3</span> <a name="P2S3"></a>Even though constants are considered static members, a <span class="non-terminal"><a href="17.3.htm#constant-declaration">constant-declaration</a></span> neither requires nor allows a static modifier.</span> <span class="sentence"><span class="sentence-number">4</span> <a name="P2S4"></a>It is an error for the same modifier to appear multiple times in a constant declaration.</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 type of a <span class="non-terminal"><a href="17.3.htm#constant-declaration">constant-declaration</a></span> specifies the type of the members introduced by the declaration.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P3S2"></a>The type is followed by a list of <span class="non-terminal"><a href="17.3.htm#constant-declarator">constant-declarator</a></span>s, each of which introduces a new member.</span> <span class="sentence"><span class="sentence-number">3</span> <a name="P3S3"></a>A <span class="non-terminal"><a href="17.3.htm#constant-declarator">constant-declarator</a></span> consists of an identifier that names themember, followed by an "=" token, followed by a <span class="non-terminal"><a href="14.15.htm#constant-expression">constant-expression</a></span> (<a href="14.15.htm">§14.15</a>) that gives the value of the member.</span> </span><span class="locator">
Paragraph 4</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P4S1"></a>The type specified in a constant declaration must be <span class="keyword">sbyte</span>, <span class="keyword">byte</span>, <span class="keyword">short</span>, <span class="keyword">ushort</span>, <span class="keyword">int</span>, <span class="keyword">uint</span>, <span class="keyword">long</span>, <span class="keyword">ulong</span>, <span class="keyword">char</span>, <span class="keyword">float</span>, <span class="keyword">double</span>, <span class="keyword">decimal</span>, <span class="keyword">bool</span>, string, an <span class="non-terminal"><a href="11.1.htm#enum-type">enum-type</a></span>, or a <span class="non-terminal"><a href="11.2.htm#reference-type">reference-type</a></span>.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P4S2"></a>Each <span class="non-terminal"><a href="14.15.htm#constant-expression">constant-expression</a></span> must yield a value of the target type or of a type that can be converted to the target type by an implicit conversion (<a href="13.1.htm">§13.1</a>).</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 a constant must be at least as accessible as the constant itself (<a href="10.5.4.htm">§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>The value of a constant is obtained in an expression using a <span class="non-terminal"><a href="14.5.2.htm#simple-name">simple-name</a></span> (<a href="14.5.2.htm">§14.5.2</a>) or a <span class="non-terminal"><a href="14.5.4.htm#member-access">member-access</a></span> (<a href="14.5.4.htm">§14.5.4</a>).</span> </span><span class="locator">
Paragraph 7</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P7S1"></a>A constant can itself participate in a <span class="non-terminal"><a href="14.15.htm#constant-expression">constant-expression</a></span>.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P7S2"></a>Thus, a constant may be used in any construct that requires a <span class="non-terminal"><a href="14.15.htm#constant-expression">constant-expression</a></span>.</span> <span class="note">[Note: Examples of such constructs include case labels, goto case statements, enum member declarations, attributes, and other constant declarations. end note]</span> </span><span class="paragraph"><span class="note">[Note: As described in <a href="14.15.htm">§14.15</a>, a <span class="non-terminal"><a href="14.15.htm#constant-expression">constant-expression</a></span> is an expression that can be fully evaluated at compile-time. Since the only way to create a non-null value of a <span class="non-terminal"><a href="11.2.htm#reference-type">reference-type</a></span> other than string is to apply the new operator, and since the new operator is not permitted in a <span class="non-terminal"><a href="14.15.htm#constant-expression">constant-expression</a></span>, the only possible value for constants of <span class="non-terminal"><a href="11.2.htm#reference-type">reference-type</a></span>s other than string is null. end note]</span> </span><span class="locator">
Paragraph 8</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P8S1"></a>When a symbolic name for a constant value is desired, but when the type of that value is not permitted in a constant declaration, or when the value cannot be computed at compile-time by a <span class="non-terminal"><a href="14.15.htm#constant-expression">constant-expression</a></span>, a readonly field (<a href="17.4.2.htm">§17.4.2</a>) may be used instead.</span> <span class="note">[Note: The versioning semantics of const and readonly differ (<a href="17.4.2.2.htm">§17.4.2.2</a>). end note]</span> </span><span class="locator">
Paragraph 9</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P9S1"></a>A constant declaration that declares multiple constants is equivalent to multiple declarations of single constants with the same attributes, modifiers, and type.</span> <span class="example">[Example: For example <pre class="code-example">
class A
{
public const double X = 1.0, Y = 2.0, Z = 3.0;
}
</pre>is equivalent to <pre class="code-example">
class A
{
public const double X = 1.0;
public const double Y = 2.0;
public const double Z = 3.0;
}
</pre>end example]</span> </span><span class="locator">
Paragraph 10</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P10S1"></a>Constants are permitted to depend on other constants within the same program as <span class="keyword">long</span> as the dependencies are not of a circular nature.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P10S2"></a>The compiler automatically arranges to evaluate the constant declarations in the appropriate order.</span> <span class="example">[Example: In the example <pre class="code-example">
class A
{
public const int X = B.Z + 1;
public const int Y = 10;
}
class B
{
public const int Z = A.Y + 1;
}
</pre>the compiler first evaluates A.Y, then evaluates B.Z, and finally evaluates A.X, producing the values 10, 11, and 12. end example]</span> <span class="sentence"><span class="sentence-number">3</span> <a name="P10S3"></a>Constant declarations may depend on constants from other programs, but such dependencies are only possible in one direction.</span> <span class="example">[Example: Referring to the example above, if A and B were declared in separate programs, it would be possible for A.X to depend on B.Z, but B.Z could then not simultaneously depend on A.Y. 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 + -