Documente Academic
Documente Profesional
Documente Cultură
Objectives
• Present namespace
– definition
– use
– advantages
2
Motivation: related types
3
Motivation: library client
4
Motivation: name collision
5
Namespace advantages
6
Namespace
• Namespace definition:
– keyword namespace
– name
– contents enclosed in { and }
– can contain class, struct, interface, enum, delegate
7
Discontinuous namespace
8
Logical grouping
namespace Shapes
Shape
Circle
Rectangle
Line
9
Qualified access
access with
fully qualified bool Overlap(Shapes.Circle c, Shapes.Rectangle r)
names {
...
}
10
Access from same namespace
namespace Shapes
Shape and Circle {
in same namespace class Circle:Shape
so no qualification
{
needed
...
}
}
11
Name management
Shape Shape
Circle
Athlete
Rectangle
Workout
Line
12
Name management details
void Process()
{
ok Shapes.Shape a;
ok Fitness.Shape b;
...
}
13
Using directive
class Layout
{
unqualified access
bool Overlap(Circle c, Rectangle r)
to all members
{
...
}
...
}
14
Multiple using directives
15
Ambiguity with using directives
class Layout
{
void Draw()
{
error Shape a;
ok Shapes.Shape b;
ok Fitness.Shape c;
...
}
...
}
16
Using alias for class
17
Using alias for namespace
18
Using placement
ok using Shapes;
class Layout
{
error using Shapes;
19
Nested namespace
Shapes
Shape
TwoD ThreeD
Sphere
Circle
Rectangle Cube
Line Tetrahedron
20
Coding nested namespace
21
Qualified access to nested namespace
22
Using directive for nested namespace
class Layout
{
unqualified access bool Overlap(Circle c, Rectangle r)
to all members
{
...
}
...
}
23
Using is not recursive
applies only to
using Shapes;
Shapes namespace
class Layout
{
ok, since class is in Shape a;
Shapes namespace
error, both are in Circle b;
nested namespace Sphere c;
...
}
24
Global namespace
namespace Shapes
{
class Shape
{
unqualified Color c;
...
}
}
25
Summary
26