namespace
play

Namespace 2 Present namespace advantages definition Objectives - PDF document

Namespace 2 Present namespace advantages definition Objectives use Motivation: related types Types often closely related group of related types often called library class Shape shape { library ... } class


  1. Namespace

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

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

  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 (); use many types Rectangle border = new Rectangle(); from same library Line tangent = new Line (); 4

  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 geometric shape class Shape { int area; ... } athletic shape class Shape { error, which int fitness; Shape s; shape? ... } 5

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

  7. Namespace • Namespace definition: – keyword namespace – name – contents enclosed in { and } – can contain class , struct , interface , enum , delegate namespace namespace Shapes { class Shape { ... } class Circle :Shape { ... } contents class Rectangle:Shape { ... } class Line :Shape { ... } } 7

  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 namespace Shapes namespace Shapes { { { class Circle:Shape class Rectangle:Shape class Line:Shape { { { ... ... ... } } } } } } 8

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

  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 bool Overlap(Shapes.Circle c, Shapes.Rectangle r) names { ... } 10

  11. Access from same namespace • Qualification not needed when in same namespace namespace Shapes Shape and Circle { in same namespace class Circle:Shape so no qualification { needed ... } } 11

  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 12

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

  14. Using directive • Using directive allows unqualified access to namespace – syntax: using namespaceName; – convenient when members used repeatedly using directive using Shapes; class Layout { unqualified access bool Overlap(Circle c, Rectangle r) to all members { ... } ... } 14

  15. Multiple using directives • Multiple using directives do not cause an error – even if each namespace contains member with same name ok, even though using Shapes; both contain class using Fitness; named Shape 15

  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 both contain class using Shapes; named Shape using Fitness; class Layout { void Draw() { error Shape a; Shapes.Shape b; ok ok Fitness.Shape c; ... } ... } 16

  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 using MyCircle = Shapes.Circle; Shapes.Circle class Layout { void Draw() { MyCircle a; use alias ... } ... } 17

  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 using MyShapes = Shapes; Shapes namespace class Layout { void Draw() { MyShapes.Circle c; use alias ... } ... } 18

  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; ok class Layout { using Shapes; error bool Overlap(Circle c, Rectangle r) { using Shapes; error ... } ... } 19

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

  21. Coding nested namespace • Two options to define nested namespace – nesting syntax – shorthand using dot operator namespace Shapes nesting syntax { namespace TwoD { class Circle:Shape { ... } } } namespace Shapes.TwoD shorthand { class Circle:Shape { ... } } 21

  22. Qualified access to nested namespace • Can use fully qualified names to access nested namespace Shapes.TwoD.Circle a; access with fully qualified names Shapes.ThreeD.Sphere b; 22

  23. Using directive for nested namespace • Can access nested namespace with using directive using Shapes.TwoD; using directive class Layout { unqualified access bool Overlap(Circle c, Rectangle r) to all members { ... } ... } 23

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

  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 in global namespace class Color { ... } namespace Shapes { class Shape { Color c; unqualified ... } } 25

  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 26

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend