2.0 C# Naming Conventions
2.0 C# Naming Conventions
C# naming conventions are an important part of C# coding standards and best practice when
you are developing .NET applications. .NET naming conventions are standards how the
naming of variables, methods, classes, and other code elements should be defined. In this
article, let us learn C# naming conventions.
Terminology
There are following three terminologies are used to declare C# and .NET naming standards.
Camel Case (camelCase): In this standard, the first letter of the word always in small
letter and after that each word starts with a capital letter. This is used for variable
names.
Pascal Case (PascalCase): In this the first letter of every word is in capital letter.
This is used for method names.
Underscore Prefix (_underScore): For underscore ( __), the word after _ use
camelCase terminology.
Native DataType
Always use native datatype instead of .NET CTS type. For example, use int instead of Int32
or Int64.
//Good
private int _salary = 100;
//Bad
private Int16 _salary = 100;
private Int32 _salary=100;
Class
Always use PascalCase for class names. Try to use noun or noun phrase for class name. Do
not give prefixes. Do not use underscores.
Methods
Always use PascalCase for method names. Use maximum 7 parameters in a method.
Always use camelCase with method arguments and local variables. Don't use Hungarian
notation for variables.
Note: Don't use abbreviations for any words and don't use underscore ( _ ) in between any
name.
Property
Use PascalCase for property. Never use Get and Set as prefix with property name.
Always use letter "I" as prefix with name of interface. After letter I, use PascalCase.
Declare member variable at the top of the class, If class has static member then it will come
at the top most and after that other member variable.
Enum
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, also called flags
enum.
Do not use an "Enum" suffix in enum type names.
Do not use "Flag" or "Flags" suffixes in enum type names.
Do not use a prefix on enumeration value names.
enum MailType
{
Html,
PlainText,
Attachment
}
Namespace
Events Names
Events are associated with actions. Therefore, events are named with verbs. For example,
Loaded, Clicked, and Printing.
Give events names with a concept of before, current, and after, using the present and
past tenses. Depending on the page, window, control, or class, the event names for a
page can be, Initialized, PreRender, Rendering, PostRender, and Exited. A button
event can be OnClick.
Event handlers use "EventHandler" suffix, as shown in the following example:
public delegate void ClickedEventHandler(object sender, ClickedEventArgs e);
Use two parameters named sender and e in event handlers.
Name event argument classes with the "EventArgs" suffix.
Fields Names
Assemblies or DLLs are created for a major functionality such as a math library.
Naming Parameters
Use names based on a parameter’s meaning rather than the parameter’s type.
Naming Resources