280 likes | 549 Vues
Namespace. Objectives. Present namespace definition use advantages. Motivation: related types. Types often closely related group of related types often called library. class Shape { ... }. shape library. class Circle:Shape { ... }. class Rectangle:Shape { ... }.
E N D
Objectives • Present namespace • definition • use • advantages
Motivation: related types • Types often closely related • group of related types often called library class Shape { ... } shape library class Circle:Shape { ... } class Rectangle:Shape { ... } class Line:Shape { ... }
Motivation: library client • Client code typically uses many types from same library • library designer should indicate types are related • simplifies search for appropriate type Circle range = new Circle (); Rectangle border = new Rectangle(); Line tangent = new Line (); use many types from same library
Motivation: name collision • Error to multiply define symbol within same scope • Shape, Rectangle, Type, Device, etc. are common names • same name often used in different libraries • duplicate names collide if libraries used together class Shape { int area; ... } geometric shape class Shape { int fitness; ... } athletic shape Shape s; error, which shape?
Namespace advantages • Namespace provides two main advantages • logical grouping of source code • name management
Namespace • Namespace definition: • keyword namespace • name • contents enclosed in { and } • can contain class, struct, interface, enum, delegate namespace Shapes { class Shape { ... } class Circle :Shape { ... } class Rectangle:Shape { ... } class Line :Shape { ... } } namespace contents
Discontinuous namespace • Namespace members can be added separately • all logically merged into single namespace Shape.cs namespace Shapes { class Shape { ... } } Circle.cs Rectangle.cs Line.cs namespace Shapes { class Circle:Shape { ... } } namespace Shapes { class Rectangle:Shape { ... } } namespace Shapes { class Line:Shape { ... } }
Logical grouping • Namespace provides logical grouping • user knows namespace members related namespace Shapes Shape Circle Rectangle Line
Qualified access • Members accessed with name of namespace and dot operator • called fully qualified name • required when accessed from outside namespace access with fully qualified names bool Overlap(Shapes.Circle c, Shapes.Rectangle r) { ... }
Access from same namespace • Qualification not needed when in same namespace namespace Shapes { class Circle:Shape { ... } } Shape and Circle in same namespace so no qualification needed
Name management • Namespace provides name management • classes in different namespaces can have same name namespace Shapes namespace Fitness Shape Shape Circle Athlete Rectangle Workout Line
Name management details • Fully qualified names are unique • avoids name collision in user code void Process() { Shapes.Shape a; Fitness.Shape b; ... } ok ok
Using directive • Using directive allows unqualified access to namespace • syntax: using namespaceName; • convenient when members used repeatedly using Shapes; class Layout { bool Overlap(Circle c, Rectangle r) { ... } ... } using directive unqualified access to all members
Multiple using directives • Multiple using directives do not cause an error • even if each namespace contains member with same name using Shapes; using Fitness; ok, even though both contain class named Shape
Ambiguity with using directives • Multiple using directives may result in ambiguity • error to attempt unqualified access to duplicated name • must use fully qualified name using Shapes; using Fitness; class Layout { void Draw() { Shape a; Shapes.Shape b; Fitness.Shape c; ... } ... } both contain class named Shape error ok ok
Using alias for class • Using allows creation of alias for class • syntax: using newClass = oldClass; • can yield more convenient or more meaningful name • helps resolve ambiguity created by multiple using directives create alias for Shapes.Circle using MyCircle = Shapes.Circle; class Layout { void Draw() { MyCircle a; ... } ... } use alias
Using alias for namespace • Using allows creation of alias for namespace • syntax: using newNamespace = oldNamespace; • can create more convenient or meaningful name • can allow easier switch to different namespace create alias for Shapes namespace using MyShapes = Shapes; class Layout { void Draw() { MyShapes.Circle c; ... } ... } use alias
Using placement • Using statements typically placed at top of file • can put inside other namespace definition • cannot go after any namespace definition • cannot put inside class or method using Shapes; class Layout { using Shapes; bool Overlap(Circle c, Rectangle r) { using Shapes; ... } ... } ok error error
Nested namespace • Namespaces can be nested Shapes Shape TwoD ThreeD Sphere Circle Cube Rectangle Line Tetrahedron
Coding nested namespace • Two options to define nested namespace • nesting syntax • shorthand using dot operator namespace Shapes { namespace TwoD { class Circle:Shape { ... } } } nesting syntax namespace Shapes.TwoD { class Circle:Shape { ... } } shorthand
Qualified access to nested namespace • Can use fully qualified names to access nested namespace Shapes.TwoD.Circle a; Shapes.ThreeD.Sphere b; access with fully qualified names
Using directive for nested namespace • Can access nested namespace with using directive using Shapes.TwoD; class Layout { bool Overlap(Circle c, Rectangle r) { ... } ... } using directive unqualified access to all members
Using is not recursive • Using directive allows access to just one namespace • does not give access to nested namespaces • must have separate directive for each applies only to Shapes namespace using Shapes; class Layout { Shape a; Circle b; Sphere c; ... } ok, since class is in Shapes namespace error, both are in nested namespace
Global namespace • Types not placed in a namespace go in the global namespace • members available for use in all other namespaces • no qualification needed class Color { ... } in global namespace namespace Shapes { class Shape { Color c; ... } } unqualified
Summary • Namespace provides name management • use helps avoid name collisions • Namespace provides logical grouping mechanism • related types grouped together • Using provides shorthand access to namespace members • provided there is no ambiguity