📄 19.6.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="19.5.htm"><img src="previous.gif" alt="previous" border="0" /></a><a href="20.htm"><img src="next.gif" alt="next" border="0" /></a></span><span class="clause-depth"><a href="18.htm"><img src="previous.gif" alt="previous at this level" border="0" /></a><a href="20.htm"><img src="next.gif" alt="next at this level" border="0" /></a> <span class="clause-number-link"><a href="19.htm">19</a></span><span class="clause-title-previous"> Arrays</span></span><span class="clause-depth"><a href="19.5.htm"><img src="previous.gif" alt="previous at this level" border="0" /></a><a href="19.6.htm"><img src="next.gif" alt="next at this level" border="0" /></a> <span class="clause-number">19.6</span><span class="clause-title"> Array initializers</span></span><span class="locator">
Paragraph 1</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P1S1"></a>Array initializers may be specified in field declarations (<a href="17.4.htm">§17.4</a>), local variable declarations (<a href="15.5.1.htm">§15.5.1</a>), and array creation expressions (<a href="14.5.10.2.htm">§14.5.10.2</a>): <span class="grammar-production"><span class="name"><a name="array-initializer"></a>array-initializer</span> : <span class="rhs"><span class="terminal">{</span> <span class="non-terminal"><a href="19.6.htm#variable-initializer-list">variable-initializer-list</a></span><sub>opt</sub> <span class="terminal">}</span> </span><span class="rhs"><span class="terminal">{</span> <span class="non-terminal"><a href="19.6.htm#variable-initializer-list">variable-initializer-list</a></span> <span class="terminal">,</span> <span class="terminal">}</span> </span></span><span class="grammar-production"><span class="name"><a name="variable-initializer-list"></a>variable-initializer-list</span> : <span class="rhs"><span class="non-terminal"><a href="19.6.htm#variable-initializer">variable-initializer</a></span> </span><span class="rhs"><span class="non-terminal"><a href="19.6.htm#variable-initializer-list">variable-initializer-list</a></span> <span class="terminal">,</span> <span class="non-terminal"><a href="19.6.htm#variable-initializer">variable-initializer</a></span> </span></span><span class="grammar-production"><span class="name"><a name="variable-initializer"></a>variable-initializer</span> : <span class="rhs"><span class="non-terminal"><a href="14.14.htm#expression">expression</a></span> </span><span class="rhs"><span class="non-terminal"><a href="19.6.htm#array-initializer">array-initializer</a></span> </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>An array initializer consists of a sequence of variable initializers, enclosed by "{"and "}" tokens and separated by "," tokens.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P2S2"></a>Each variable initializer is an expression or, in the case of a multi-dimensional array, a nested array initializer.</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 context in which an array initializer is used determines the type of the array being initialized.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P3S2"></a>In an array creation expression, the array type immediately precedes the initializer.</span> <span class="sentence"><span class="sentence-number">3</span> <a name="P3S3"></a>In a field or variable declaration, the array type is the type of the field or variable being declared.</span> <span class="sentence"><span class="sentence-number">4</span> <a name="P3S4"></a>When an array initializer is used in a field or variable declaration, </span><span class="example">[Example: such as: <pre class="code-example">
int[] a = {0, 2, 4, 6, 8};
</pre>end example]</span> it is simply shorthand for an equivalent array creation expression: <span class="example">[Example: <pre class="code-example">
int[] a = new int[] {0, 2, 4, 6, 8};
</pre>end example]</span> </span><span class="paragraph"><span class="sentence"><span class="sentence-number">5</span> <a name="P3S5"></a>For a single-dimensional array, the array initializer must consist of a sequence of expressions that are assignment compatible with the element type of the array.</span> <span class="sentence"><span class="sentence-number">6</span> <a name="P3S6"></a>The expressions initialize array elements in increasing order, starting with the element at index zero.</span> <span class="sentence"><span class="sentence-number">7</span> <a name="P3S7"></a>The number of expressions in the array initializer determines the length of the array instance being created.</span> <span class="example">[Example: For example, the array initializer above creates an int[] instance of length 5 and then initializes the instance with the following values: <pre class="code-example">
a[0] = 0; a[1] = 2; a[2] = 4; a[3] = 6; a[4] = 8;
</pre>end example]</span> </span><span class="paragraph"><span class="sentence"><span class="sentence-number">8</span> <a name="P3S8"></a>For a multi-dimensional array, the array initializer must have as many levels of nesting as there are dimensions in the array.</span> <span class="sentence"><span class="sentence-number">9</span> <a name="P3S9"></a>The outermost nesting level corresponds to the leftmost dimension and the innermost nesting level corresponds to the rightmost dimension.</span> <span class="sentence"><span class="sentence-number">10</span> <a name="P3S10"></a>The length of each dimension of the array is determined by the number of elements at the corresponding nesting level in the array initializer.</span> <span class="sentence"><span class="sentence-number">11</span> <a name="P3S11"></a>For each nested array initializer, the number of elements must be the same as the other array initializers at the same level.</span> <span class="example">[Example: The example: <pre class="code-example">
int[,] b = {{0, 1}, {2, 3}, {4, 5}, {6, 7}, {8, 9}};
</pre>creates a two-dimensional array with a length of five for the leftmost dimension and a length of two for the rightmost dimension: <pre class="code-example">
int[,] b = new int[5, 2];
</pre>and then initializes the array instance with the following values: <pre class="code-example">
b[0, 0] = 0; b[0, 1] = 1;
b[1, 0] = 2; b[1, 1] = 3;
b[2, 0] = 4; b[2, 1] = 5;
b[3, 0] = 6; b[3, 1] = 7;
b[4, 0] = 8; b[4, 1] = 9;
</pre>end example]</span> </span><span class="paragraph"><span class="sentence"><span class="sentence-number">12</span> <a name="P3S12"></a>When an array creation expression includes both explicit dimension lengths and an array initializer, the lengths must be constant expressions and the number of elements at each nesting level must match the corresponding dimension length.</span> <span class="example">[Example: Here are some examples: <pre class="code-example">
int i = 3;
int[] x = new int[3] {0, 1, 2}; // OK
int[] y = new int[i] {0, 1, 2}; // Error, i not a constant
int[] z = new int[3] {0, 1, 2, 3}; // Error, length/initializer mismatch
</pre></span></span><span class="paragraph"><span class="example">Here, the initializer for y results in a compile-time error because the dimension length expression is not a constant, and the initializer for z results in a compile-time error because the length and the number of elements in the initializer do not agree. 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 + -