Defining Your Own Namespaces

The syntax for defining a namespace of your own making is straightforward. Simply write the keyword namespace followed by a namespace name you've chosen. Below this header, include a pair of curly braces; they delimit the scope of your namespace. You can then include the classes you want this namespace to contain by writing their definitions between these curly braces. The source code of Listing 15.1 for example, defines a namespace called ElevatorSimulation that contains an Elevator and a Person class. The scope of the ElevatorSimulation namespace spans from line 2 to line 36.

Listing 15.1. Defining the ElevatorSimulation namespace
 01: namespace ElevatorSimulation 02: { 03: using System; 04: 05: class Elevator ...

Get C# Primer Plus now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.