Solution Naming Guidelines
C# and VB.net naming conventions play a crucial role in ensuring code readability, maintainability, and consistency across projects. These conventions assist developers in quickly understanding the purpose and usage of code elements like classes, methods, variables, and more. Adhering to established naming standards/conventions promotes teamwork and facilitates smoother code reviews and maintenance.
General Guidelines
- Choose easily readable, preferably grammatically correct names. For example, HorizontalAlignment is more readable than AlignmentHorizontal.
- Favor readability and clarity over brevity and abbreviated names.
- Avoid using names that conflict with reserved keywords of used programming languages.
- Use PascalCase for class names, file names, namespaces, all method names, and public member names. Use camelCase for private fields, parameters and local variables. See below for reference:
Name | Case Type | Example |
---|---|---|
Namespace | Pascal | namespace CompanyName.ProjectName{…} |
Class/Type | Pascal | public class ClassName {…} |
Interface | Pascal | public interface IBusinessService {…} |
Method | Pascal | private void MethodName() |
Property/Field | Pascal | public int PropertyName {get; set;} |
Enum | Pascal | public enum ErrorLevel |
Enum Member | Pascal | CriticalLevel |
Constant | Snake, All Caps | public const string TEST_BASE_URL |
Parameters | Camel | private string GetParameter(string parameterName) |
Local Variables | Camel | var localVariable = “Test” |
Struct | Pascal | public struct StructName |
Resource Key | Pascal | SaveButtonTooltipText |
- Do not use abbreviations. For example, use
ButtonOnClick
rather thanBtnOnClick
. - Name a Variable in a most descriptive way possible. Example: if a
SessionInfo
variable that is passed to other calls, is allowed to benull
, name it sessionInfoOrNull instead of the usual sessionInfo. - Name Classes as Nouns or Noun phrases.
- Do not prefix class names (ex. “
CMyDatabaseObject
”). - End the name of a derived class with the name of the base class.
- Name Interfaces as adjective phrases or occasionally as nouns or noun phrases.
- Prefix Interfaces with the letter "I", to indicate that the type is an interface.
- Name Methods as Verbs or Verb phrases.
- Name Properties and Fields as a Nouns, Noun phrases, or Adjectives.
- Do not prefix property names with Get or Set; these typically indicate that the property should be a method.
- Name collection properties with a plural phrase; not something like ItemList or ItemCollection.
- Name Boolean properties with a positive phrase (
CanSeek
instead ofCantSeek
). - Use
var
to declare a variable if the type can be easily inferred from the name, otherwise use the type. - Use syntactically interesting names, rather than a language-specific keywords for type names. For example,
GetLength
is a better name thanGetInt
. - Use a singular type name for an enumeration unless its values are bit fields.
- Use a plural type name for an enumeration with bit fields as values.
- Do not use “Enum” or “Flag(s)” as a suffix for an enum type name.
- Do not use a prefix for a name on an enumeration value.
- Name Events as a Verb or Verb Phrase (ex.
Clicked
,Show
,ShowDialog
). - Use -ing and -ed to express pre-events and post-events. For example:
- Deleting: Occurs just before the object is getting deleted.
- Delete: Occurs when the object needs to be deleted by the event handler.
- Deleted: Occurs when the object is already deleted.
- Prefix an event handler with "On". For example, a method that handles its own Closing event should be named
OnClosing
. - Use descriptive names for Parameters.
- Use parameter names based on its meaning rather than its type.
- Postfix asynchronous methods with Async or TaskAsync