---
title: "C# Coding Standard and Naming Convention"  
description: "Anyone can write code with a few months of programming experience.But some developers know what is the coding standard and naming conventions but ever"  
author: "Amit Kumar"  
published: 2017-03-27  
updated: 2019-03-31  
canonical: https://www.mindstick.com/articles/12270/c-sharp-coding-standard-and-naming-convention  
category: "c#"  
tags: ["c#"]  
reading_time: 7 minutes  

---

# C# Coding Standard and Naming Convention

Anyone can write code with a few months of programming experience.But some developers know what is the coding standard and [naming convention](https://www.mindstick.com/articles/23223/naming-convention-or-coding-standard) but everyone not follow the coding standard.

#### 1. Introduction

In this article we will learn what is the coding standard and [naming conventions](https://www.mindstick.com/forum/145555/what-are-some-common-naming-conventions-used-in-programming) in C#.Firstly arrise a question in developers mind that,

##### What is the definition of good code ?

**Ans**: According to me, When code is more understandable , readable, maintainable , review and reusing code this is the main characteristics of good code. Review Code: This is the major point of code review. Inside it, review the code make sure that the code follow the coding standard or not.

##### Naming Conventions:

1. Pascal Case2. Camel Case Pascal Case: First character of all words are Upper Case and other characters are lower case.Example: CaseHistory Camel Case: First letter is in lowercase and first letter of every subsequent concatenated word is in caps.ORFirst character of all words, except the first word are Upper Case and other characters are lower case. \
Example: businessCase\

##### 2. Naming Conventions and Standards

2.1 Use Pascal casing for Class names.

##### Example

1. public class BusinessCase
2. {
3. //.........
4. }

2.2 Use Pascal casing for [Method names](https://www.mindstick.com/forum/157800/what-happens-in-case-of-the-inherited-interfaces-have-conflicting-method-names).

##### Example

1. void AutoComplete(string name)
2. {
3. // ......
4. }

2.3 Use Camel casing for variables and [method parameters](https://www.mindstick.com/forum/161700/what-is-the-difference-between-ref-and-out-keywords-in-method-parameters).

##### Example

1. int totalCount = 0;
2. void AutoComplete(string name)
3. {
4. string fullMessage = "Hello " + name;
5. }

2.4 Use Meaningful, descriptive words to name variables. Do not use abbreviations.

##### Example

1. Good:
2. string address;
3. int salary;
4.
5. Bad:
6. string nam;
7. string addr;
8. int sal;

2.4 Do not use single character [variable names](https://www.mindstick.com/forum/160065/restrictions-imposed-by-strict-mode-on-the-usage-of-reserved-keywords-and-variable-names) like i, n, s etc. Use names like index, temp One exception in this case would be variables used for iterations in loops.

##### Example

1. for (int i = 0; i < count; i++)
2. {
3. //...........
4. }

2.6 Do not use underscores (_) for [local variable](https://answers.mindstick.com/qa/104742/explain-local-variable) names. 2.7 Prefix boolean variables, properties and methods with "is" or similar prefixes.

1. Ex: private bool _isFinished

2.8 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 element. A brief list is given below. ****Example****

| ## Control | ## Prefix |
| --- | --- |
| Label | lbl |
| TextBox | txt |
| DataGrid | dtg |
| Button | btn |
| ImageButton | imb |

2.9 Use Pascal Case for file names.

## 3. Indentation and Spacing:

3.1 Use TAB for indentation. Do not use SPACES. Define the Tab size as 4. 3.2 Comments should be in the same level as the code (use the same level of indentation).

##### Example

##### \
Good

1. // Format a message and display
2. string fullMessage = "Hello my name is " + name;
3. DateTime currentTime = DateTime.Now;
4. string message = fullMessage + ", the time is : " + currentTime.ToShortTimeString();
5. MessageBox.Show( message );

##### Bad

1. // Format a message and display
2. string fullMessage = " Hello my name is " + name;
3. DateTime currentTime = DateTime.Now;
4. string message = fullMessage + ", the time is : " + currentTime.ToShortTimeString();
5. MessageBox.Show ( message );

3.3 Curly braces ( {} ) should be in the same level as the code outside the braces. 3.4 Use one blank line to separate logical groups of code.

##### Example

##### Good

1. bool ShowMessage ( string name )
2. {
3. string fullName = "Hello " + name;
4. DateTime currentTime = DateTime.Now;
5.
6. string message = fullName + ", the time is : " + currentTime.ToShortTimeString();
7.
8. MessageBox.Show ( message );
9.
10. if ( ... )
11. {
12. // Write your code here
13. // ...
14.
15. return false;
16. }
17.
18. return true;
19. }

##### Bad

1. bool ShowMessage (string name)
2. {
3. string fullName = "Hello " + name;
4. DateTime currentTime = DateTime.Now;
5. string message = fullName + ", the time is : " + currentTime.ToShortTimeString();
6. MessageBox.Show ( message );
7. if ( ... )
8. {
9. // Write your code here
10. // ...
11. return false;
12. }
13. return true;

3.5 There should be one and only one single blank line between each method inside the class. 3.6 The curly braces should be on a separate line and not in the same line as if, for etc.******Example****** ******Good******

1. if ( ... )
2. {
3. // write your code here
4. }

******Bad******

1. if ( ... ) {
2. // Write your code here }

3.7 Use a single space before and after each operator and brackets.

##### Example

##### Good

1. if(showResult)
2. {
3. for ( int i = 0; i < 10; i++ )
4. {
5. //
6. }
7. }

##### Bad

1. if(showResult==true)
2. {
3. for(int i= 0;i<10;i++)
4. {
5. //
6. }
7. }

**4. Superb Programming Practices** 4.1 [Method name](https://www.mindstick.com/forum/33583/selector-creation-with-method-name-and-parameter) should meaning full means what they do. Do not use mis-leading names. If the method name meaning full then, there is no need of documentation.

##### Example

##### Good

1. void SaveStudentDetails ( string studentDetails )
2. {
3. // Save the student details.
4. }

##### Bad

1. // This method will save the student details.
2. void SaveDetails ( string student )
3. {
4. // Save the student details.
5. }

4.2 A method should only one job at a time. Do not use for more than one job.

##### Example

##### Good

1. //Save student details
2. SaveStudentDetails();
3.
4. //Send email to user that student details is added successfully.
5. SendEmail();

##### Bad

1. //Save student details
2. Public void SaveStudentDetails()
3. {
4. // First task
5. //Save student details
6.
7. //Second task
8. // Send emails
9. }

4.3 Make sure for string comparison [convert string](https://www.mindstick.com/forum/23050/how-to-convert-string-value-to-hexa-value-in-vb-dot-net) to uppercase or lower case for compare.

##### Example

##### Good

1. If(UserName.ToLower()=="Tom")
2. {
3. // write yor logic here
4. }

##### Bad

1. If(UseName=="TOm")
2. {
3. //
4. }

##### 5. Comments

5.1 Do not write comments for every line of code and every variable declared. 5.2 Use // or /// for comments. Avoid using /* … */ 5.3 Write comments where is it required. But good readable and very less comments. If all variables and method names are perfect meaningful, then there is no need many comments. 5.4 If you initialize a numeric variable to a special number other than 0, -1 etc, document the reason for choosing that value.\
5.5 Make sure perform spelling check on comments and make sure proper grammar and punctuation is used.

##### 6.Enum

Always use singular noun for define enum.

##### Example

1. Enum Mailtype
2. {
3. Subject,
4. Body,
5. PlaneText
6. }

##### 7.Interface

Always use letter "I" as prefix with name of interface. After letter I, use PascalCase. ******Example******

1. public interface IMultiply
2. {
3. int Multiplication();
4. }

##### Conclusion:

In this article, I have shown you how to use C# coding standard and naming conventions . Hope you understood and liked it.

---

Original Source: https://www.mindstick.com/articles/12270/c-sharp-coding-standard-and-naming-convention

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
