0% found this document useful (0 votes)
40 views9 pages

Declarations: © University of Linz, Institute For System Software, 2004 Published Under The Microsoft Curriculum License

The document discusses declaration spaces in programming languages. Declaration spaces refer to the scope or region in which declarations are valid. The document outlines different types of declaration spaces such as namespaces, classes, methods, and blocks. It also describes scoping and visibility rules for names declared in different spaces.

Uploaded by

api-3734769
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views9 pages

Declarations: © University of Linz, Institute For System Software, 2004 Published Under The Microsoft Curriculum License

The document discusses declaration spaces in programming languages. Declaration spaces refer to the scope or region in which declarations are valid. The document outlines different types of declaration spaces such as namespaces, classes, methods, and blocks. It also describes scoping and visibility rules for names declared in different spaces.

Uploaded by

api-3734769
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 9

Declarations

© University of Linz, Institute for System Software, 2004


published under the Microsoft Curriculum License
1
Declaration Space
The program area to which a declaration belongs

Kinds of declaration spaces


- namespace: declarations of classes, interfaces, structs, enums, delegates
- class, interface, struct: declarations of fields, methods, ...
- enumeration: declarations of enumeration constants
- method block: declarations of local variables

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 {...}

Equally named namespaces in different files constitute a single declaration space.


Nested namespaces constitute a declaration space on their own.
4
Using Other Namespaces
Color.cs Figures.cs Triangle.cs
namespace Util { namespace Util.Figures { namespace Util.Figures {
public enum Color {...} public class Rect {...} public class Triangle {...}
} public class Circle {...} }
}

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)

Most programs need the namespace System => using System;


5
Classes, Interfaces, Structs
class C { // applies also to structs
... fields, constants ...
... methods ...
... constructors, destructors ...
... properties ...
... indexers ...
... events ...
... overloaded operators ...
... nested types (classes, interfaces, structs, enumerations, delegates) ...
}

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
}

You might also like