📄 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="10.2.htm"><img src="previous.gif" alt="previous" border="0" /></a><a href="10.4.htm"><img src="next.gif" alt="next" border="0" /></a></span><span class="clause-depth"><a href="9.htm"><img src="previous.gif" alt="previous at this level" border="0" /></a><a href="11.htm"><img src="next.gif" alt="next at this level" border="0" /></a> <span class="clause-number-link"><a href="10.htm">10</a></span><span class="clause-title-previous"> Basic concepts</span></span><span class="clause-depth"><a href="10.2.htm"><img src="previous.gif" alt="previous at this level" border="0" /></a><a href="10.4.htm"><img src="next.gif" alt="next at this level" border="0" /></a> <span class="clause-number">10.3</span><span class="clause-title"> Declarations</span></span><span class="locator">
Paragraph 1</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P1S1"></a>Declarations in a C# program define the constituent elements of the program.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P1S2"></a>C# programs are organized using namespaces (<a href="16.htm">§16</a>), which can contain type declarations and nested namespace declarations.</span> <span class="sentence"><span class="sentence-number">3</span> <a name="P1S3"></a>Type declarations (<a href="16.5.htm">§16.5</a>) are used to define classes (<a href="17.htm">§17</a>), structs (<a href="18.htm">§18</a>), interfaces (<a href="20.htm">§20</a>), enums (<a href="21.htm">§21</a>), and delegates (<a href="22.htm">§22</a>).</span> <span class="sentence"><span class="sentence-number">4</span> <a name="P1S4"></a>The kinds of members permitted in a type declaration depend on the form of the type declaration.</span> <span class="sentence"><span class="sentence-number">5</span> <a name="P1S5"></a>For instance, class declarations can contain declarations for constants (<a href="17.3.htm">§17.3</a>), fields (<a href="17.4.htm">§17.4</a>), methods (<a href="17.5.htm">§17.5</a>), properties (<a href="17.6.htm">§17.6</a>), events (<a href="17.7.htm">§17.7</a>), indexers (<a href="17.8.htm">§17.8</a>), operators (<a href="17.9.htm">§17.9</a>), instance constructors (<a href="17.10.htm">§17.10</a>), destructors (<a href="17.12.htm">§17.12</a>), static constructors (<a href="17.11.htm">§17.11</a>), and nested types.</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 declaration defines a name in the declaration space to which the declaration belongs.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P2S2"></a>Except for overloaded members (<a href="10.6.htm">§10.6</a>), it is a compile-time error to have two or more declarations that introduce members with the same name in a declaration space.</span> <span class="sentence"><span class="sentence-number">3</span> <a name="P2S3"></a>However, no diagnostic is required if the declaration space is a namespace for the global declaration space and the conflicting declarations are in separate programs.</span> <span class="sentence"><span class="sentence-number">4</span> <a name="P2S4"></a>It is never possible for a declaration space to contain different kinds of members with the same name.</span> <span class="example">[Example: For example, a declaration space can never contain a field and a method by the same name. end example]</span> </span><span class="locator">
Paragraph 3</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P3S1"></a>There are several different types of declaration spaces, as described in the following.</span> <ul><li><span class="sentence"><span class="sentence-number">2</span> <a name="P3S2"></a> Within all source files of a program, <span class="non-terminal"><a href="16.4.htm#namespace-member-declaration">namespace-member-declaration</a></span>s with no enclosing <span class="non-terminal"><a href="16.2.htm#namespace-declaration">namespace-declaration</a></span> are members of a single combined declaration space called the global declaration space.</span> </li><li><span class="sentence"><span class="sentence-number">3</span> <a name="P3S3"></a> Within all source files of a program, <span class="non-terminal"><a href="16.4.htm#namespace-member-declaration">namespace-member-declaration</a></span>s within <span class="non-terminal"><a href="16.2.htm#namespace-declaration">namespace-declaration</a></span>s that have the same fully qualified namespace name are members of a single combined declaration space.</span> </li><li><span class="sentence"><span class="sentence-number">4</span> <a name="P3S4"></a> Each class, struct, or interface declaration creates a new declaration space.</span> <span class="sentence"><span class="sentence-number">5</span> <a name="P3S5"></a>Names are introduced into this declaration space through <span class="non-terminal"><a href="17.2.htm#class-member-declaration">class-member-declaration</a></span>s, <span class="non-terminal"><a href="18.2.htm#struct-member-declaration">struct-member-declaration</a></span>s, or <span class="non-terminal"><a href="20.2.htm#interface-member-declaration">interface-member-declaration</a></span>s.</span> <span class="sentence"><span class="sentence-number">6</span> <a name="P3S6"></a>Except for overloaded instance constructor declarations and static constructor declarations, a class or struct member declaration cannot introduce a member by the same name as the class or struct.</span> <span class="sentence"><span class="sentence-number">7</span> <a name="P3S7"></a>A class, struct, or interface permits the declaration of overloaded methods and indexers.</span> <span class="sentence"><span class="sentence-number">8</span> <a name="P3S8"></a>Furthermore, a class or struct permits the declaration of overloaded instance constructors and operators.</span> <span class="example">[Example: For example, a class, struct, or interface may contain multiple method declarations with the same name, provided these method declarations differ in their signature (<a href="10.6.htm">§10.6</a>). end example]</span> <span class="sentence"><span class="sentence-number">9</span> <a name="P3S9"></a>Note that base classes do not contribute to the declaration space of a class, and base interfaces do not contribute to the declaration space of an interface.</span> <span class="sentence"><span class="sentence-number">10</span> <a name="P3S10"></a>Thus, a derived class or interface is allowed to declare a member with the same name as an inherited member.</span> <span class="sentence"><span class="sentence-number">11</span> <a name="P3S11"></a>Such a member is said to hide the inherited member.</span> </li><li><span class="sentence"><span class="sentence-number">12</span> <a name="P3S12"></a> Each enumeration declaration creates a new declaration space.</span> <span class="sentence"><span class="sentence-number">13</span> <a name="P3S13"></a>Names are introduced into this declaration space through <span class="non-terminal"><a href="21.3.htm#enum-member-declaration">enum-member-declaration</a></span>s.</span> </li><li><span class="sentence"><span class="sentence-number">14</span> <a name="P3S14"></a> Each block or <span class="non-terminal"><a href="15.7.2.htm#switch-block">switch-block</a></span> creates a different declaration space for local variables.</span> <span class="sentence"><span class="sentence-number">15</span> <a name="P3S15"></a>Names are introduced into this declaration space through <span class="non-terminal"><a href="15.5.1.htm#local-variable-declaration">local-variable-declaration</a></span>s.</span> <span class="sentence"><span class="sentence-number">16</span> <a name="P3S16"></a>If a block is the body of an instance constructor, method, or operator declaration, or a get or set accessor for an indexer declaration, the parameters declared in such a declaration are members of the block's local variable declaration space.</span> <span class="sentence"><span class="sentence-number">17</span> <a name="P3S17"></a>The local variable declaration space of a block includes any nested blocks.</span> <span class="sentence"><span class="sentence-number">18</span> <a name="P3S18"></a>Thus, within a nested block it is not possible to declare a local variable with the same name as a local variable in an enclosing block.</span> </li><li><span class="sentence"><span class="sentence-number">19</span> <a name="P3S19"></a> Each block or <span class="non-terminal"><a href="15.7.2.htm#switch-block">switch-block</a></span> creates a separate declaration space for labels.</span> <span class="sentence"><span class="sentence-number">20</span> <a name="P3S20"></a>Names are introduced into this declaration space through <span class="non-terminal"><a href="15.4.htm#labeled-statement">labeled-statement</a></span>s, and the names are referenced through <span class="non-terminal"><a href="15.9.3.htm#goto-statement">goto-statement</a></span>s.</span> <span class="sentence"><span class="sentence-number">21</span> <a name="P3S21"></a>The label declaration space of a block includes any nested blocks.</span> <span class="sentence"><span class="sentence-number">22</span> <a name="P3S22"></a>Thus, within a nested block it is not possible to declare a label with the same name as a label in an enclosing block.</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 textual order in which names are declared is generally of no significance.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P4S2"></a>In particular, textual order is not significant for the declaration and use of namespaces, constants, methods, properties, events, indexers, operators, instance constructors, destructors, static constructors, and types.</span> <span class="sentence"><span class="sentence-number">3</span> <a name="P4S3"></a>Declaration order is significant in the following ways: </span><ul><li><span class="sentence"><span class="sentence-number">4</span> <a name="P4S4"></a> Declaration order for field declarations and local variable declarations determines the order in which their initializers (if any) are executed.</span> </li><li><span class="sentence"><span class="sentence-number">5</span> <a name="P4S5"></a> Local variables must be defined before they are used (<a href="10.7.htm">§10.7</a>).</span> </li><li><span class="sentence"><span class="sentence-number">6</span> <a name="P4S6"></a> Declaration order for enum member declarations (<a href="21.3.htm">§21.3</a>) is significant when <span class="non-terminal"><a href="14.15.htm#constant-expression">constant-expression</a></span> values are omitted.</span> </li></ul></span><span class="paragraph"><span class="example">[Example: The declaration space of a namespace is "open ended", and two namespace declarations with the same fully qualified name contribute to the same declaration space. For example <pre class="code-example">
namespace Megacorp.Data
{
class Customer
{
...
}
}
namespace Megacorp.Data
{
class Order
{
...
}
}
</pre></span></span><span class="paragraph"><span class="example">The two namespace declarations above contribute to the same declaration space, in this case declaring two classes with the fully qualified names Megacorp.Data.Customer and Megacorp.Data.Order. Because the two declarations contribute to the same declaration space, it would have caused a compile-time error if each contained a declaration of a class with the same name. end example]</span> </span><span class="paragraph"><span class="note">[Note: As specified above, the declaration space of a block includes any nested blocks. Thus, in the following example, the F and G methods result in a compile-time error because the name i is declared in the outer block and cannot be redeclared in the inner block. However, the H and I methods are valid since the two i's are declared in separate non-nested blocks. <pre class="code-example">
class A
{
void F() {
int i = 0;
if (true) {
int i = 1;
}
}
void G() {
if (true) {
int i = 0;
}
int i = 1;
}
void H() {
if (true) {
int i = 0;
}
if (true) {
int i = 1;
}
}
void I() {
for (int i = 0; i < 10; i++)
H();
for (int i = 0; i < 10; i++)
H();
}
}
</pre>end note]</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 + -