100% found this document useful (1 vote)
256 views

C# Coding Standards and Naming Conventions

C# Coding Standards and Naming Conventions - Dofactory.com

Uploaded by

ratneshkumarg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
256 views

C# Coding Standards and Naming Conventions

C# Coding Standards and Naming Conventions - Dofactory.com

Uploaded by

ratneshkumarg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

5/17/2018 C# Coding Standards and Naming Conventions - dofactory.

com

Login (/login.aspx) Join Now (/join.aspx)

(/)

C# Coding Standards and Naming Conventions

Below are our C# coding standards, naming conventions, and best practices.
Use these in your own projects and/or adjust these to your own needs.

do use PascalCasing for class names and method names.

1. public class ClientActivity


2. {
3. public void ClearStatistics()
4. {
5. //...
6. }
7. public void CalculateStatistics()
8. {
9. //...
10. }
11. }

Why: consistent with the Microsoft's .NET Framework and easy to read.

do use camelCasing for method arguments and local variables.

 

http://www.dofactory.com/reference/csharp-coding-standards 1/12
5/17/2018 C# Coding Standards and Naming Conventions - dofactory.com

1. public class UserLog


2. {
3. public void Add(LogEvent logEvent)
4. {
5. int itemCount = logEvent.Items.Count;
6. // ...
7. }
8. }

Why: consistent with the Microsoft's .NET Framework and easy to read.

do not use Hungarian notation or any other type identification in identifiers

1. // Correct
2. int counter;
3. string name;
4.
5. // Avoid
6. int iCounter;
7. string strName;

Why: consistent with the Microsoft's .NET Framework and Visual Studio IDE makes determining types very easy
(via tooltips). In general you want to avoid type indicators in any identifier.

do not use Screaming Caps for constants or readonly variables

1. // Correct
2. public static const string ShippingType = "DropShip";
3.
4. // Avoid
5. public static const string SHIPPINGTYPE = "DropShip";

Why: consistent with the Microsoft's .NET Framework. Caps grap too much attention.

 

http://www.dofactory.com/reference/csharp-coding-standards 2/12
5/17/2018 C# Coding Standards and Naming Conventions - dofactory.com

avoid using Abbreviations. Exceptions: abbreviations commonly used as names, such as Id, Xml, Ftp, Uri

1. // Correct
2. UserGroup userGroup;
3. Assignment employeeAssignment;
4.
5. // Avoid
6. UserGroup usrGrp;
7. Assignment empAssignment;
8.
9. // Exceptions
10. CustomerId customerId;
11. XmlDocument xmlDocument;
12. FtpHelper ftpHelper;
13. UriPart uriPart;

Why: consistent with the Microsoft's .NET Framework and prevents inconsistent abbreviations.

do use PascalCasing for abbreviations 3 characters or more (2 chars are both uppercase)

1. HtmlHelper htmlHelper;
2. FtpTransfer ftpTransfer;
3. UIControl uiControl;

Why: consistent with the Microsoft's .NET Framework. Caps would grap visually too much attention.

do not use Underscores in identifiers. Exception: you can prefix private static variables
with an underscore.

 

http://www.dofactory.com/reference/csharp-coding-standards 3/12
5/17/2018 C# Coding Standards and Naming Conventions - dofactory.com

1. // Correct
2. public DateTime clientAppointment;
3. public TimeSpan timeLeft;
4.
5. // Avoid
6. public DateTime client_Appointment;
7. public TimeSpan time_Left;
8.
9. // Exception
10. private DateTime _registrationDate;

Why: consistent with the Microsoft's .NET Framework and makes code more natural to read (without 'slur'). Also
avoids underline stress (inability to see underline).

do use predefined type names instead of system type names like Int16, Single, UInt64, etc

1. // Correct
2. string firstName;
3. int lastIndex;
4. bool isSaved;
5.
6. // Avoid
7. String firstName;
8. Int32 lastIndex;
9. Boolean isSaved;

Why: consistent with the Microsoft's .NET Framework and makes code more natural to read.

do use implicit type var for local variable declarations. Exception: primitive types (int, string,
double, etc) use predefined names.

 

http://www.dofactory.com/reference/csharp-coding-standards 4/12
5/17/2018 C# Coding Standards and Naming Conventions - dofactory.com

1. var stream = File.Create(path);


2. var customers = new Dictionary();
3.
4. // Exceptions
5. int index = 100;
6. string timeSheet;
7. bool isCompleted;

Why: removes clutter, particularly with complex generic types. Type is easily detected with Visual Studio tooltips.

do use noun or noun phrases to name a class.

1. public class Employee


2. {
3. }
4. public class BusinessLocation
5. {
6. }
7. public class DocumentCollection
8. {
9. }

Why: consistent with the Microsoft's .NET Framework and easy to remember.

do prefix interfaces with the letter I. Interface names are noun (phrases) or adjectives.

1. public interface IShape


2. {
3. }
4. public interface IShapeCollection
5. {
6. }
7. public interface IGroupable
8. {
9. }

consistent with the Microsoft's .NET Framework.


Why: 

http://www.dofactory.com/reference/csharp-coding-standards 5/12
5/17/2018 C# Coding Standards and Naming Conventions - dofactory.com

do name source files according to their main classes. Exception: file names with partial classes
reflect their source or purpose, e.g. designer, generated, etc.

1. // Located in Task.cs
2. public partial class Task
3. {
4. //...
5. }

1. // Located in Task.generated.cs
2. public partial class Task
3. {
4. //...
5. }

Why: consistent with the Microsoft practices. Files are alphabetically sorted and partial classes remain adjacent.

do organize namespaces with a clearly defined structure

1. // Examples
2. namespace Company.Product.Module.SubModule
3. namespace Product.Module.Component
4. namespace Product.Layer.Module.Group

Why: consistent with the Microsoft's .NET Framework. Maintains good organization of your code base.

do vertically align curly brackets.

 

http://www.dofactory.com/reference/csharp-coding-standards 6/12
5/17/2018 C# Coding Standards and Naming Conventions - dofactory.com

1. // Correct
2. class Program
3. {
4. static void Main(string[] args)
5. {
6. }
7. }

Why: Microsoft has a different standard, but developers have overwhelmingly preferred vertically aligned brackets.

do declare all member variables at the top of a class, with static variables at the very top.

1. // Correct
2. public class Account
3. {
4. public static string BankName;
5. public static decimal Reserves;
6.
7. public string Number {get; set;}
8. public DateTime DateOpened {get; set;}
9. public DateTime DateClosed {get; set;}
10. public decimal Balance {get; set;}
11.
12. // Constructor
13. public Account()
14. {
15. // ...
16. }
17. }

Why: generally accepted practice that prevents the need to hunt for variable declarations.

do use singular names for enums. Exception: bit field enums.

 

http://www.dofactory.com/reference/csharp-coding-standards 7/12
5/17/2018 C# Coding Standards and Naming Conventions - dofactory.com

1. // Correct
2. public enum Color
3. {
4. Red,
5. Green,
6. Blue,
7. Yellow,
8. Magenta,
9. Cyan
10. }
11.
12. // Exception
13. [Flags]
14. public enum Dockings
15. {
16. None = 0,
17. Top = 1,
18. Right = 2,
19. Bottom = 4,
20. Left = 8
21. }

Why: consistent with the Microsoft's .NET Framework and makes the code more natural to read. Plural flags
because enum can hold multiple values (using bitwise 'OR').

do not explicitly specify a type of an enum or values of enums (except bit fields)

1. // Don't
2. public enum Direction : long
3. {
4. North = 1,
5. East = 2,
6. South = 3,
7. West = 4
8. }
9.
10. // Correct
11. public enum Direction
12. {
13. North,
14. East,
15. South,
16. West
17. }
 

http://www.dofactory.com/reference/csharp-coding-standards 8/12
5/17/2018 C# Coding Standards and Naming Conventions - dofactory.com

Why: can create confusion when relying on actual types and values.

do not suffix enum names with Enum

1. // Don't
2. public enum CoinEnum
3. {
4. Penny,
5. Nickel,
6. Dime,
7. Quarter,
8. Dollar
9. }
10.
11. // Correct
12. public enum Coin
13. {
14. Penny,
15. Nickel,
16. Dime,
17. Quarter,
18. Dollar
19. }

Why: consistent with the Microsoft's .NET Framework and consistent with prior rule of no type indicators in
identifiers.

Better Code
Better Career
Better Lifestyle

.NET
Design Pattern
Framework 4.5
 (/products/net-design- 
pattern-framework)
http://www.dofactory.com/reference/csharp-coding-standards 9/12
5/17/2018 C# Coding Standards and Naming Conventions - dofactory.com

(/products/net-design-pattern-
framework)
C# and VB (/products/net-
design-pattern-framework)

Includes:

Gang of Four Patterns


Head First Patterns
Enterprise Patterns
Multi-Tier Patterns

Repository Pattern
Unit-of-Work Pattern
Active Record Pattern
CQRS Pattern

Model View Controller


Model View Presenter
Model View ViewModel

SparkTM RAD Platform


Art Shop MVC
application
More...

 

http://www.dofactory.com/reference/csharp-coding-standards 10/12
5/17/2018 C# Coding Standards and Naming Conventions - dofactory.com

(/products/net-design-
pattern-framework)
Get started today!

Click here for details


(/products/net-design-pattern-
framework)

-- Instant Access --
Instant Download

Company

About Us (/about)
Our Story (/story)
Services (/services)
Training (/training)
Contact Us (/contact)

Privacy (/privacy)
Terms (/terms)
Licensing (/licensing)

Customers

Our Customers (/customers)


Customer Stories (/customers/stories)

Community

Questions (/topic/search.aspx)
Ask Question (/topic/add.aspx)
Explore (/topic/topics.aspx)
Tags (/tag/tags.aspx) 
Users (/user/users.aspx)

http://www.dofactory.com/reference/csharp-coding-standards 11/12
5/17/2018 C# Coding Standards and Naming Conventions - dofactory.com

Reference Guides

.NET Design Patterns (/net/design-patterns)


JavaScript Design Patterns (/javascript/design-patterns)

JavaScript Tutorial (/tutorial/javascript)


SQL Tutorial (/sql/tutorial)

Connection Strings (/reference/connection-strings)


Visual Studio Shortcuts (/reference/visual-studio-shortcuts)
C# Coding Standards (/reference/csharp-coding-standards)
HTML Colors (/reference/html-color-codes)

Our Products

.NET Design Pattern Framework (/products/net-design-pattern-framework) TM


PRO .NET Design Pattern Framework (/products/pro-net-design-pattern-framework) TM

JavaScript + jQuery Pattern Framework (/products/javascript-jquery-design-pattern-framework) TM


SQL + Database Pattern Framework (/products/sql-database-design-pattern-framework) TM

Products and Pricing (/products)

© 2018 - Data & Object Factory, LLC. dofactory.com. All rights reserved.

 

http://www.dofactory.com/reference/csharp-coding-standards 12/12

You might also like