Declarations: © University of Linz, Institute For System Software, 2004 Published Under The Microsoft Curriculum License
Declarations: © University of Linz, Institute For System Software, 2004 Published Under The Microsoft Curriculum License
namespace N {
...
class C {
...
void Foo() {
...
if (...) { Statement blocks are not declaration spaces on their own
... but belong to the declaration space of the enclosing
} method block
}
}
}
2
Rules
Scoping rules
- No name may be declared more than once in the same declaration space on the same level.
- However, it may be redeclared in an inner declaration space (except in a nested statement
block)
Visibility rules
- A name is visible in its whole declaration space (local variables only from the point of their
declaration onwards). This implies that the use of a name may precede its declaration
(except for local variables, which must be declared before they are used)
- If a name is redeclared in an inner declaration space, it hides the same name from the outer
declaration space.
- In general, no name is visible outside its declaration space.
- The visibility of names declared in namespaces, classes, structs and interfaces can be
controlled by the modifiers public, private, protected and internal.
- Names of enumeration constants can only be accessed if they are qualified with their
enumeration type name.
3
Namespaces
File: X.cs
namespace A {
... classes ...
... interfaces ...
... structs ...
... enumerations ...
... delegates ...
namespace B { // full name:
A.B
...
} }
File: Y.cs
namespace A {
...
namespace B {...}
}
namespace C {...}
using Util.Figures;
class Test {
Rect r; // without qualification (because of using Util.Figures)
Triangle t;
Util.Color c; // with qualification
}
Foreign namespaces
• must either be imported (e.g. using Util;)
• or specified in a qualified name (e.g. Util.Color)
interface IX {
... methods ...
... properties ...
... indexers ...
... events ...
}
The declaration space of a subclass does not belong to the declaration space of its base class
=> it is ok to declare the same names in a base class and in its subclasses.
6
Enumerations
enum E {
... enumeration constants ...
}
7
Statement Blocks
Kinds of blocks
void Foo (int x) { // method block B1
... local variables ...
if (...) { // nested block
B2
... local variables ...
}
for (int i = 0; ...) { // nested block
B3
... local variables ...
}
}
The declaration space of a block includes the declaration spaces of its nested blocks.
B1 B1
B2 B3
• Formal parameters belong to the declaration space of their method block.
• The loop variable of a for statement belongs to the block of this for statement.
• The declaration of a local variable must precede the use of this variable.
8
Declaration of Local Variables
void Foo(int a) {
int b;
if (...) {
int b; // error: b is already declared in the outer block
int c;
int d;
...
} else {
int a; // error: a is already declared in the outer block (parameter)
int d; // ok: no conflict with d in the if block
}
for (int i = 0; ...) {...}
for (int i = 0; ...) {...} // ok: no conflict with i from the previous loop
int c; // error: c is already declared in a nested block
}