1 / 26

Namespace

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

frances
Download Presentation

Namespace

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Namespace

  2. Objectives • Present namespace • definition • use • advantages

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

  4. 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

  5. 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?

  6. Namespace advantages • Namespace provides two main advantages • logical grouping of source code • name management

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

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

  9. Logical grouping • Namespace provides logical grouping • user knows namespace members related namespace Shapes Shape Circle Rectangle Line

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

  11. 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

  12. 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

  13. Name management details • Fully qualified names are unique • avoids name collision in user code void Process() { Shapes.Shape a; Fitness.Shape b; ... } ok ok

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

  20. Nested namespace • Namespaces can be nested Shapes Shape TwoD ThreeD Sphere Circle Cube Rectangle Line Tetrahedron

  21. 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

  22. 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

  23. 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

  24. 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

  25. 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

  26. 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

More Related