22.2 Mechanics

Mechanics, in this case, refers to the process of selecting a name for a given metadata token in the language such as class, interface, field, parameter, and so on.

Microsoft, in its "Naming Guidelines" topic (under the ".NET Framework Design Guidelines" in the .NET SDK Documentation), contains a list of "Do's" and "Don'ts" regarding naming guidelines. Rather than reprint that list here in either parroted or paraphrased form, we just make the following general suggestions.

Names should be descriptive and concise. More importantly, they should be meaningful to the principal consumers of the nameuse syntax, phraseology, and terms that are familiar to the developers who will use the code you are writing.

As suggested, since classes and interfaces and value types are often marked "public," use Pascal-case for their names.

Interfaces should always be prefixed with I, following in COM tradition. Following the Pascal-casing rules, make sure the next letter of the interface is also capitalized, as in IComparable (indicating an interface that provides comparable behavior). Frequently, interfaces are also descriptive names (Comparable, Disposable, and so on), rather than nouns.

Other than the I for interfaces, do not use any sort of prefixes on names. This is what namespaces are for.

Attributes should always end in the Attribute suffix. The C# compiler can use shorthand naming rules (allowing you to leave off the Attribute portion of the name when using the attribute) if you follow this convention.

Enums should use Pascal-casing, both for the typename and the values within the enumerated type.

Fields should be named relative to what they store. Microsoft specifically recommends against using Hungarian notation, even the MFC m_ style; however, this style shows up in a few places inside the FCL. Use what feels right to you and your peers, but be consistent.

Events should be suffixed with EventHandler; event argument types should be suffixed with EventArgs. Frequently, events are prefixed with an On, indicating the event is fired "on button clicks" or "on window closing"; this is not entirely consistent, however.

Exceptions should always be suffixed with Exception.

    Part II: Programming with the .NET Framework
    Part IV: API Quick Reference
    Evaluation has ГЦМµёЕexpired.