Organizing Types

A C# program is basically a group of types. These types are defined in files, organized by namespaces, compiled into modules, and then grouped into an assembly.

Generally, these organizational units overlap: an assembly can contain many namespaces, and a namespace can be spread across several assemblies. A module can be part of many assemblies, and an assembly can contain many modules. A source file can contain many namespaces, and a namespace can span many source files. For more information, see Section 3.9 in Chapter 3.

Files

File organization is of almost no significance to the C# compiler: an entire project can be merged into a single .cs file and still compile successfully (preprocessor statements are the only exception to this). However, it’s generally tidy to have one type in one file, with a filename that matches the name of the class and a directory name that matches the name of the class’s namespace.

Namespaces

Namespace declaration syntax:

namespace name + Namespaces {
using-statement *
[ namespace-declaration | type-declaration ]*Namespaces
}
Namespaces Dot-delimited.
Namespaces No delimiters.

Get C# Essentials 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.