Anyone can write code with a few months of programming experience. But only a few developers know what coding standards and naming conventions are, but not everyone follows best practices.
Introduction
In this article, we will learn what the coding standards and naming conventions are in C#. Firstly, you might wonder:
What is the definition of good code?
Answer
According to me, when the code is more understandable, readable, maintainable, reviewed and reused are the main characteristics of good code.
Review Code
This is the major point of code review. Inside it, review the code to make sure that the code follows the coding standard.
Naming Conventions
- Pascal Case
- Camel Case
Pascal Case
The first characters of all words are Upper Case and other characters are lower case.
Example
CaseHistory
Camel Case
The first letter is in lowercase and the first letter of every subsequent concatenated word is in caps.
OR
The first character of all words, except the first word, is Upper Case and other characters are lower case.
Example
businessCase
Naming Conventions and Standards
Use Pascal casing for Class names.
Example
The first characters of all words are Upper Case and other characters are lower case.
Example
CaseHistory
Camel Case
The first letter is in lowercase and the first letter of every subsequent concatenated word is in caps.
OR
The first character of all words, except the first word, is Upper Case and other characters are lower case.
Example
businessCase
Naming Conventions and Standards
Use Pascal casing for Class names.
Example
- public class BusinessCase
- {
- //.........
- }
Example
- void AutoComplete(string name)
- {
- // ......
- }
Example
- int totalCount = 0;
- void AutoComplete(string name)
- {
- string fullMessage = "Hello " + name;
- }
Example
- Good:
- string address;
- int salary;
- Bad:
- string nam;
- string addr;
- int sal;
One exception, in this case, would be variables used for iterations in loops.
Example
- for (int i = 0; i < count; i++)
- {
- //...........
- }
Prefix boolean variables, properties, and methods with "is" or similar prefixes.
- Ex: private bool _isFinished
Use appropriate prefix for the UI elements so that you can identify them from the rest of the variables.
Use appropriate prefix for each of the UI elements. A brief list is given below.
Example
| Control | Prefix |
| Label | lbl |
| TextBox | txt |
| DataGrid | dtg |
| Button | btn |
| ImageButton | imb |
Use Pascal Case for file names.
Indentation and Spacing
Use TAB for indentation. Do not use SPACES. Define the Tab size as 4.
Comments should be in the same level as the code (use the same level of indentation).
Example
Good
Comments should be in the same level as the code (use the same level of indentation).
Example
Good
- // Format a message and display
- string fullMessage = "Hello my name is " + name;
- DateTime currentTime = DateTime.Now;
- string message = fullMessage + ", the time is : " + currentTime.ToShortTimeString();
- MessageBox.Show( message );
Bad
- // Format a message and display
- string fullMessage = " Hello my name is " + name;
- DateTime currentTime = DateTime.Now;
- string message = fullMessage + ", the time is : " + currentTime.ToShortTimeString();
- MessageBox.Show ( message );
Curly braces ( {} ) should be in the same level as the code outside the braces.
Use one blank line to separate logical groups of code.
Example
Good
Use one blank line to separate logical groups of code.
Example
Good
- bool ShowMessage(string name) {
- string fullName = "Hello " + name;
- DateTime currentTime = DateTime.Now;
- string message = fullName + ", the time is : " + currentTime.ToShortTimeString();
- MessageBox.Show(message);
- if (...) {
- // Write your code here
- // ...
- return false;
- }
- return true;
- }
Bad
- bool ShowMessage(string name) {
- string fullName = "Hello " + name;
- DateTime currentTime = DateTime.Now;
- string message = fullName + ", the time is : " + currentTime.ToShortTimeString();
- MessageBox.Show(message);
- if (...) {
- // Write your code here
- // ...
- return false;
- }
- return true;
There should be one and only one single blank line between each method inside the class.
The curly braces should be on a separate line and not in the same line as if, for, etc.
Example
Good
The curly braces should be on a separate line and not in the same line as if, for, etc.
Example
Good
- if ( ... )
- {
- // write your code here
- }
Bad
- if ( ... ) {
- // Write your code here }
Use a single space before and after each operator and brackets.
Example
Good
Example
Good
- if(showResult)
- {
- for ( int i = 0; i < 10; i++ )
- {
- //
- }
- }
Bad
- if(showResult==true)
- {
- for(int i= 0;i<10;i++)
- {
- //
- }
- }
Superb Programming Practices
Method name should clarify the meaning. Do not use misleading names. If the method name is clear then there is no need of documentation.
Example
Good
Method name should clarify the meaning. Do not use misleading names. If the method name is clear then there is no need of documentation.
Example
Good
- void SaveStudentDetails (string studentDetails )
- {
- // Save the student details.
- }
Bad
- // This method will save the student details.
- void SaveDetails (string student )
- {
- // Save the student details.
- }
A method should do only one job at a time. Do not use it for more than one job.
Example
Good
Example
Good
- //Save student details
- SaveStudentDetails();
- //Send email to user that student details is added successfully.
- SendEmail();
Bad
- //Save student details
- Public void SaveStudentDetails()
- {
- // First task
- //Save student details
- //Second task
- // Send emails
- }
Make sure string comparisons convert string to uppercase or lowercase for comparison.
Example
Good
Example
Good
- If(UserName.ToLower()=="tom")
- {
- // write yor logic here
- }
Bad
- If(UseName=="TOm")
- {
- //
- }
Comments
- Do not write comments for every line of code and every variable declared.
- Use // or /// for comments. Avoid using /* … */
- Write comments where is it required. But they should be good readable and limited. If all variables and method names are perfectly meaningful, then there is no need of many comments.
- If you initialize a numeric variable to a special number other than 0, -1 etc, document the reason for choosing that value.
- Make sure to perform spelling check on comments and make sure proper grammar and punctuation is used.
Enum
Always use a singular noun for defining enum.
Example
- Enum Mailtype
- {
- Subject,
- Body,
- PlaneText
- }
Interface
Always use the letter "I" as a prefix with the name of an interface. After letter I, use PascalCase.
Example
Always use the letter "I" as a prefix with the name of an interface. After letter I, use PascalCase.
Example
- public interface IMultiply
- {
- int Multiplication();
- }
Conclusion
In this article, I have shown you how to use C# coding standards and naming conventions. I hope you understood and liked it.
In this article, I have shown you how to use C# coding standards and naming conventions. I hope you understood and liked it.

Jim SnyderPosted Dec 10, 2019, 2:43 PM
"Do not use abbreviations." is not always a good best practice. Often, a variable name collides with reserved words and must be either made longer to be more descriptive or shorter. I'll use as an example "string". Both "tempString" and "str" fit the bill. The problem with best practices is that they are never black and white.
Jim SnyderPosted Dec 10, 2019, 2:34 PM
One missing one is that functions should start with a verb. This would be like "GetXxxx", "EncryptXxxx", "CalculateXxxx", "CreateXxxx", etc.
Jim SnyderPosted Dec 10, 2019, 2:28 PM
"'Comments should be in the same level as the code (use the same level of indentation)." Both good and bad are the same.
Khaja MoizuddinPosted Jun 3, 2018, 4:18 AM
Good one for beginners.
Michael TimoshinPosted May 28, 2018, 3:00 AM
"Always use a singular noun for defining enum". In case of [Flags] that's a bad practice.
Michael TimoshinPosted May 28, 2018, 2:58 AM
What about UserName.Equals("Tom", StringComparison.OrdinalIgnoreCase)?
Mike PetrisPosted May 21, 2018, 4:57 AM
Indentation example seems wrong. There are just two spaces more: <"Hello/" Hello> and <Show( message/Show ( message>
alex gurovPosted May 17, 2018, 8:50 AM
UserName.ToLower()=="Tom" ?
krishna mohanPosted May 15, 2018, 7:13 AM
If(UserName.ToLower()=="Tom"). How it can be true ?
Kartik PawarPosted May 13, 2018, 1:00 AM
Thank you for sharing...
Anbu ManiPosted May 13, 2018, 12:41 AM
Very Nice Buddy :-)