Namespace 2 Present namespace advantages definition Objectives - - PDF document

namespace
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Namespace

slide-2
SLIDE 2

Objectives

  • Present namespace

– definition – use – advantages

2

slide-3
SLIDE 3

Motivation: related types

  • Types often closely related

– group of related types often called library

3

class Shape { ... } shape library class Circle:Shape { ... } class Rectangle:Shape { ... } class Line:Shape { ... }

slide-4
SLIDE 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

4

Circle range = new Circle (); Rectangle border = new Rectangle(); Line tangent = new Line ();

use many types from same library

slide-5
SLIDE 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?

5

slide-6
SLIDE 6

Namespace advantages

  • Namespace provides two main advantages

– logical grouping of source code – name management

6

slide-7
SLIDE 7

Namespace

  • Namespace definition:

– keyword namespace – name – contents enclosed in { and } – can contain class, struct, interface, enum, delegate

7

namespace Shapes { class Shape { ... } class Circle :Shape { ... } class Rectangle:Shape { ... } class Line :Shape { ... } }

namespace contents

slide-8
SLIDE 8

Discontinuous namespace

  • Namespace members can be added separately

– all logically merged into single namespace

namespace Shapes { class Shape { ... } } Shape.cs namespace Shapes { class Circle:Shape { ... } } Circle.cs namespace Shapes { class Rectangle:Shape { ... } } Rectangle.cs namespace Shapes { class Line:Shape { ... } } Line.cs

8

slide-9
SLIDE 9

Logical grouping

  • Namespace provides logical grouping

– user knows namespace members related

namespace Shapes

Shape Circle Rectangle Line

9

slide-10
SLIDE 10

Qualified access

  • Members accessed with name of namespace and dot operator

– called fully qualified name – required when accessed from outside namespace

bool Overlap(Shapes.Circle c, Shapes.Rectangle r) { ... }

access with fully qualified names

10

slide-11
SLIDE 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

11

slide-12
SLIDE 12

Name management

  • Namespace provides name management

– classes in different namespaces can have same name

12

namespace Shapes

Shape Circle Rectangle Line

namespace Fitness

Shape Athlete Workout

slide-13
SLIDE 13

Name management details

  • Fully qualified names are unique

– avoids name collision in user code

void Process() { Shapes.Shape a; Fitness.Shape b; ... }

  • k
  • k

13

slide-14
SLIDE 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

14

slide-15
SLIDE 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;

  • k, even though

both contain class named Shape

15

slide-16
SLIDE 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

16

using Shapes; using Fitness; class Layout { void Draw() { Shape a; Shapes.Shape b; Fitness.Shape c; ... } ... } error

  • k
  • k

both contain class named Shape

slide-17
SLIDE 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

using MyCircle = Shapes.Circle; class Layout { void Draw() { MyCircle a; ... } ... } use alias create alias for Shapes.Circle

17

slide-18
SLIDE 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

using MyShapes = Shapes; class Layout { void Draw() { MyShapes.Circle c; ... } ... } use alias create alias for Shapes namespace

18

slide-19
SLIDE 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; ... } ... }

  • k

error error

19

slide-20
SLIDE 20

Nested namespace

  • Namespaces can be nested

TwoD

Shape Circle Rectangle Line

ThreeD

Sphere Cube Tetrahedron

Shapes

20

slide-21
SLIDE 21

Coding nested namespace

  • Two options to define nested namespace

– nesting syntax – shorthand using dot operator

21

namespace Shapes { namespace TwoD { class Circle:Shape { ... } } }

nesting syntax

namespace Shapes.TwoD { class Circle:Shape { ... } }

shorthand

slide-22
SLIDE 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

22

slide-23
SLIDE 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

23

slide-24
SLIDE 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

using Shapes; class Layout { Shape a; Circle b; Sphere c; ... }

applies only to Shapes namespace

  • k, since class is in

Shapes namespace error, both are in nested namespace

24

slide-25
SLIDE 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

25

slide-26
SLIDE 26

Summary

26

  • 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