Coding Style GuideTopic 1 of 5~5 min

File Organization

Developer Spends 45 Minutes Searching for Method; It Was in a File Named 'Stuff.cs'

Illustration of file organization chaos vs order

Dear Marilyn: Does it really matter how I organize my files? The compiler doesn't care.

— Chaotic in Chicago

Dear Chaotic: The compiler also doesn't care if you name all your variables x1, x2, and x3. That doesn't make it a good idea. File organization is about human navigation, not machine parsing.

The Sacred Order of File Contents

Every C# file should follow this structure, in this exact order:

  1. 1. Using directives (external namespaces)
  2. 2. Namespace declaration
  3. 3. Class/Interface declaration
  4. 3.1 Constants and static fields
  5. 3.2 Private fields
  6. 3.3 Constructors
  7. 3.4 Properties
  8. 3.5 Public methods
  9. 3.6 Private methods

Why this order? Because when someone opens your file, they want to understand the "what" (fields, properties) before the "how" (methods). It's like reading a recipe: ingredients first, then instructions.

The One Class, One File Rule

Each source file should contain exactly one class, and the filename should match the class name exactly.

Correct

CustomerService.cs

Contains: class CustomerService

Wrong

Utilities.cs

Contains: 47 unrelated classes

Quick Check

You have a class called 'OrderProcessor'. What should the file be named?