1. Introduction
This document provides the C# coding standards for development. The goal is to define guidelines to enforce a consistent style and formatting and help developers avoid common pitfalls and mistakes.
Consistency is the key to maintainable code. This statement is most true for naming your projects, source files, and identifiers including variables, properties, methods, parameters, classes, interfaces and namespaces.
| Identifier Type | Rules for Naming | Example |
|
Namespaces |
Pascal Case; should start with the company namespace, followed by application "layers" as applicable |
CoCon.DataServices.Contacts |
|
Classes |
Nouns in Pascal Case (also use for structs) |
class Contact |
|
Exception Classes |
Pascal Case and always end with the suffix Exception |
Class CoConGeneralException |
|
Interfaces |
Pascal Case prefixed by "I" |
interface ICustomer |
|
Methods |
Verb or Verb/Noun pair in Pascal Case |
GetBackground(); |
|
Properties |
Pascal Case; do not prefix with get or set |
FirstName |
|
Constants |
All uppercase with words separated by underscores |
public const int MIN_WIDTH = 4; |
|
Enum Type |
Hungarian notation. |
enuHatType |
|
Enum Value |
All uppercase with words separated by underscores |
BOWLER TOP_HAT |
|
Instance Variable |
Camel Case, prefixed with "m"; always make private and accessible via a property with the applicable scope. |
private int mCurrentWidth; |
|
Local Variable |
Camel Case. Always name the variable appropriately, for example what it is being used for. Avoid using single characters like "x" or "y" except in FOR loops. Never name a variable the same as the type. |
// Wrong AppUser appUser; // Correct AppUser appUserData; |
|
Parameters |
Camel Case Never name a parameter the same as the type. |
public void GetContact(int contactId); |
|
Web Page |
Pascal Case (Very important since Visual Studio automatically creates Code Behind class on this name.) |
ShowResults.aspx |
|
Web page controls |
Camel Case with control type prefix. For user controls, prefix an appropriately named control with "uc". |
lbFirstName txtFirstName ddCountry ucPopupDisplay |
2.1 Case sensitivity
To avoid confusion regarding the use of case sensitivity, do not create any of the following:
- Two namespaces with names that differ only by case.
- A method with parameter names that differ only by case.
- Methods with names that only differ by a single letter or number.
- A type with property names that differ only by case.
- A type with method names that differ only by case.
- Variables with a type that differs only by case.
- Variables matching a data type using a different case.
3. Coding Style
Consistent layout, format, and organization are keys to creating maintainable code. The following sections describe the preferred way to implement source code to create readable, clear, and consistent code that is easy to understand and maintain.
3.1 Formatting
- Never declare more than 1 namespace per file.
- Avoid putting multiple classes in a single file.
- Always place curly braces ({ and }) on a new line.
- Always use curly braces ({ and }) in conditional statements.
Exampleif (contactCount == 0)
{
// code for true case
}
else
{
// code for false case
}
- Always put parenthesis around all clauses in if statements.
Example
Bad
if (contactCount == 0 && retrievingData)
{
}
Good
if ((contactCount == 0) &&
(retrievingData))
{
}
- Never make method calls within a clause of a if statement
Example
Bad
if (GetContacts().Count == 0)
{
}
Good
List<Contact> contactList = GetContacts();
if (contactList.Count == 0)
{
}
- Do not use elseif to string if statements together.
- Always use a Tab and Indention size of 2, inserted as spaces.
- Declare each variable independently, not in the same statement.
Example
Bad
string firstName, lastName;
Good
string firstName
string lastName;
- Place namespace "using" statements together at the top of the file. Group .NET namespaces above custom namespaces. Organize these namespaces alphabetically. NOTE: Visual Studio provides tools to sort using statements in the desired order.
- Group internal class implementation by type in the following order using #region statements:
- Attributes
- Constructors
- Properties
- Methods
- Order declarations within type groups based upon access modifier and visibility:
- Public
- Protected
- Internal
- Private
- Order methods within the public/protected/private sections alphabetically.
- Segregate interface implementation using #region statements.
- Recursively indent all code blocks contained within braces. NOTE: Visual Studio supports formatting code by Edit/Advanced/Format Code (or control-K, control-D). This requires setting the editor tabbing and spacing as indicated above.
- Use white space (CR/LF, Tabs, and so on) liberally to separate and organize code.
- Avoid declaring multiple attribute declarations within a single line. Instead stack each attribute as a separate declaration. This is required in all scopes (assembly, type, method, member, parameter, and so on).
Example
Bad
[Attribute1] [Attribute1] [Attribute1]
publicclassMyClass
{
}
Good
[Attribute1]
[Attribute1]
[Attribute1]
publicclassMyClass
{
}
- The "this" and "base" keywords should be used when needed to specify the namespace of an object used, and not as part of general practice.
- If in doubt, always err on the side of clarity and consistency.
- Parameters in method declarations should be on separate lines and all parameters should be left-aligned.
Example
publicstaticvoid Update(Contact contactData,
IDbConnection dbConn,
IDbTransaction dbTrans)
{
}
- Parameters in method calls should be on separate lines with all parameters left-aligned.
Example
Contact.Update(contactData,
dbConn,
dbTrans);
- Do not insert spaces in a parameter list for method declarations or method calls NOTE: This behavior is configurable in Visual Studio.
Example
Contact.Update(contactData,
dbConn,
dbTrans);
Rounded Rectangular Callout: Rounded Rectangular
Do not insert spaces here Callout: Or here
- Declare all variables at the beginning of a method. Do not place declarations throughout the method body.
- Place a "using" or "imports" statement at the beginning of each class to avoid having to type a fully qualified namespace/class/method name to invoke a method:
Example
Bad
CoCon.SystemFramework.LoggerServices.Write(…);
Good
using CoCon.SystemFramework;
…
LoggerServices.Write(…);
- Put parenthesis around the entire entity being cast.
Example
enuHatType hatType = (enuHatType)(DBService.GetInt(dataRow,
COLUMN_NAME,
DEFAULT_VALUE));
- A parent class is responsible for updating the relationship table with its child/included objects, its child objects do not address that relationship.
3.2 Commenting

jubula samalPosted Nov 22, 2022, 6:41 AM
Excellent topic
Sagar Pandurang KapPosted Jan 7, 2020, 11:54 PM
Superb article.Optimistic.Very helpful for me
Nilesh ChaudhariPosted Jan 7, 2020, 6:58 AM
Thank you...
Bidyasagar MishraPosted Jul 28, 2019, 6:59 AM
Awesome article
Tung LamPosted May 7, 2019, 11:54 AM
Thanks for sharing
Anilananda ChakraborttyPosted Apr 17, 2019, 12:42 AM
Nice article thanks for sharing! It helps me a lot!
Pradip PatilPosted Apr 10, 2019, 3:17 AM
Thanks for sharing
Le TuanPosted Apr 6, 2019, 11:05 PM
Thanks for sharing
Bruno LopesPosted Apr 6, 2019, 9:05 PM
Wow fantastic, It's a very nice article. Helps a lot! thank you!
Muneer ShaikhPosted Apr 3, 2019, 8:06 AM
Thanks for Great Tutorial
Matiullah ZahirPosted Apr 3, 2019, 3:51 AM
Great Tutorial
Former memberPosted Mar 30, 2019, 11:30 PM
Thanks for sharing
Dhiren PatelPosted Mar 29, 2019, 4:39 AM
Thanks for sharing this wonderful article.
Pavel JelínekPosted Mar 26, 2019, 9:11 AM
Nice summary, thanks. But in the "better" half of last code example, there are some issues. Firstly, 2 => "Item 3", and secondly, why do we declare a variable "message"? It would make more sense to me if WriteLine(message) was bellow the switch. As in https://www.c-sharpcorner.com/article/c-sharp-8-0-new-feature-swtich-expression/ . Thanks
RakeshPosted Mar 22, 2019, 9:48 AM
Informative
Mahesh ChandPosted Mar 19, 2019, 8:03 AM
Thanks Faisal. I think you should mention C# 8 and 7.x features. If coders use these new features, they code will be optimized, clean, readable, and faster :).
Faisal PathanPosted Mar 15, 2019, 6:54 AM
One thing forgot Nileksh Dhimer, Welcome back to C-Sharpcorner :)
Faisal PathanPosted Mar 15, 2019, 6:31 AM
var case = "first"; var message = itemSwitch switch { "First" => Console.WriteLine("First"), "Second" => Console.WriteLine("Yes you are great") }; Thanks for Read :)
Nileksh DhimerPosted Mar 15, 2019, 6:19 AM
Nice Article Faisal. But tell me how to implement this situation: switch(choice){case "First": "Second": console.write("Yes you are great");}
David MccarterPosted Mar 14, 2019, 7:18 PM
If you like or need coding standards, then I recommend the ONLY .NET coding standards book (mine) out there! https://www.amazon.in/dp/B07BJMDSMS
Talaviya BhavdipPosted Mar 14, 2019, 6:12 AM
Good article dear Faisal Pathan
Kaushik DudhatPosted Mar 11, 2019, 10:57 PM
Nice article Faisal Pathan. It is very helpful for me.