Quantcast

C# Naming Conventions (general purpose)

C# Naming Conventions (general purpose)



There are only two hard things in Computer Science: cache invalidation and naming things.
-- Phil Karlton

 

Many developers many coding styles so it is important to set a naming convention on each project where you work in a team. There isn't wrong convention but there are many ways you can approach so agree on a pattern in order to make the code intuitive.

Here is an example to take away and modify by your preferences (see below for explanations and examples):

1. Locally declared variables: Camel case
2. Method parameters: Camel case
3. Method names: Pascal case. Try to use a verb-noun pair for example GetPath(). Methods with return values - The name should reflect the return value.
4. Property names: Pascal case
5. Class names: Pascal case
6. Interface names: Pascal case starting with an I
7. Private fields: Camel case
8. Constants: Uppercase
9. Private static fields: Camel case

10. Custom exceptions: Suffix all custom exception names with Exception

11. Delegates: Suffix all event handlers with Handler; suffix everything else with Delegate.

12. Enums: Pascal Case with prefix En. Do not suffix with Enum. Try to explicitly specify the values of the enums.

13. Curly brackets: use the after each control flow statement.

Donts:

Avoid using abbreviations

Avoid undersores in identifiers except for prefix and when using Uppercase. Even then use only one in a row.

 

Uppercase

All letters in the identifier are capitalized.

Example:

MAX, PI, MIN_VAL

Separate words by underscore to avoid "Screaming Caps" which is not readable

 

Camel case

The first letter of an identifier is lowercase and the first letter of each subsequent concatenated word is capitalized.

Example:

myOwnService, isValid

 

Pascal case

The first letter in the identifier and the first letter of each subsequent concatenated word are capitalized.

Example:

DataObject, DateTime

 






;