ECMA-334 C# Language Specification10.3: Declarations |
Declarations in a C# program define the constituent elements of the program. C# programs are organized using namespaces (16), which can contain type declarations and nested namespace declarations. Type declarations (16.5) are used to define classes (17), structs (18), interfaces (20), enums (21), and delegates (22). The kinds of members permitted in a type declaration depend on the form of the type declaration. For instance, class declarations can contain declarations for constants (17.3), fields (17.4), methods (17.5), properties (17.6), events (17.7), indexers (17.8), operators (17.9), instance constructors (17.10), destructors (17.12), static constructors (17.11), and nested types.
A declaration defines a name in the declaration space to which the declaration belongs. Except for overloaded members (10.6), it is a compile-time error to have two or more declarations that introduce members with the same name in a declaration space. 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. It is never possible for a declaration space to contain different kinds of members with the same name.
There are several different types of declaration spaces, as described in the following.
namespace-member-declaration
s with no enclosing namespace-declaration
are members of a single combined declaration space called the global declaration space. namespace-member-declaration
s within namespace-declaration
s that have the same fully qualified namespace name are members of a single combined declaration space. class-member-declaration
s, struct-member-declaration
s, or interface-member-declaration
s. 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. A class, struct, or interface permits the declaration of overloaded methods and indexers. Furthermore, a class or struct permits the declaration of overloaded instance constructors and operators. enum-member-declaration
s. switch-block
creates a different declaration space for local variables. Names are introduced into this declaration space through local-variable-declaration
s. 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. The local variable declaration space of a block includes any nested blocks. 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. switch-block
creates a separate declaration space for labels. Names are introduced into this declaration space through labeled-statement
s, and the names are referenced through goto-statement
s. The label declaration space of a block includes any nested blocks. Thus, within a nested block it is not possible to declare a label with the same name as a label in an enclosing block. The textual order in which names are declared is generally of no significance. 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. Declaration order is significant in the following ways:
constant-expression
values are omitted.
namespace Megacorp.Data
{
class Customer
{
...
}
}
namespace Megacorp.Data
{
class Order
{
...
}
}
end note]
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();
}
}