I am going to describe here several rules and conventions which are useful for developing applications and class libraries in C#. Here, the main goal is to define guidelines to enforce the consistent style and formatting to help developers avoid common mistakes while coding in C#.
Naming Conventions
General Guidelines
- Always use Camel Case or Pascal Case names for naming conventions.
- Avoid ALL CAPS and all lowercase names. Although single lowercase words or letters are acceptable.
- Do not use names that begin with a numeric character.
- Always choose meaningful and specific names.
- Do not use abbreviations unless the full name is enormous.
- Avoid including the parent class name within a property name.
Example:
Customer.Name //Good
Customer.CustomerName //Bad - It is better to prefix Boolean variables and properties with “Can”, “Is” or “Has” like “CanPopulate”, “IsValid” or “HasContent”, etc.
- Do not use Hungarian Notation!
Example: strName or iCount - Any Abbreviations must be widely known and accepted and avoid abbreviations longer than 5 characters.
- For two-letter abbreviations, use uppercase and Pascal Case for longer abbreviations.
- Do not use C# reserved words as names.
- Avoid adding redundant or meaningless prefixes and suffixes to identifiers
Example:
// Bad!
public enum ColorsEnum {…}
public class CVehicle {…}
public struct RectangleStruct {…}
Name Usage & Syntax
- Class or Struct
Pascal Case
Examples:
private class MyClass
{…}For class name, use either a noun or noun phrase
For Example:
private struct ApplicationSettings
{…}When sub-classing another type, add an appropriate class-suffix when possible.
Examples:
internal class SpecializedAttribute : Attribute
{…}
public class CustomerCollection : CollectionBase
{…}
public class CustomEventArgs : EventArgs
{…} - Interface
Use Pascal Case convention.
Always prefix the interface name with a capital “I”.
Example:
interface ICustomer
{…} - Method
Pascal Case.
Try to use a Verb or Verb-Object pair.
Example:
public void Execute()
{…}
private string GetAssemblyVersion(Assembly target)
{…} - Property
Pascal Case.
Never prefix property names with “Get” or “Set”.
Example:
public string Name
{
get{…}
Set{…}
} - Field (Public, Protected, or Internal)
Pascal Case.
Avoid using non-private Fields! Use Properties instead.
Example:
public string Name;
protected IList InnerList - Field (Private)
Camel Case and prefix with a one underscore (_) character.
Example:
private string _name; - Variable
- Camel Case.
- Avoid using single characters like “x” or “y” except in certain loops like FOR, Foreach, etc.
- Avoid using variable names like text1, text2, text3, etc.
- Parameter
Camel Case.
Example:
public void Execute(string commandText, int iterations)
{…}
Coding Style
Formatting
- Never declare more than 1 namespace per file.
- Avoid putting multiple classes in a single file.
- Declare each variable independently – not within the same statement.
- Place namespace “using” statements together at the highest of the file. Group .NET namespaces above custom namespaces.
-
Group internal class implementation by type within the following order:
- Member variables.
- Constructors & Finalizers.
- Nested Enums, Structs, and Classes.
- Properties
- Methods
- The sequence of declarations within the type groups based upon access modifier and visibility:
- Public
- Protected
- Internal
- Private
- Segregate interface Implementation by using #region statements.
- Always try to use curly braces i.e. { and } in conditional statements.
- Always use a Tab & Indention size of 4 to separate and organize code.
- Recursively indent all code blocks contained within braces.
Code Commenting
- Use // or /// and avoid /*……. */
- Place the comment above the code in a separate line, but not just after the end of the code in the same line.
- Begin comment text with an uppercase letter and End comment text with a period.
- Insert one space between the comment delimiter (//) and therefore the comment text, as shown within the following example.
Example:
// The following declaration will create a query to map the fields.
// It will not run this query. - Do not “flowerbox” comment blocks.
Example:
// ***************************************
// Comment block
// *************************************** - Use inline-comments to elucidate assumptions, known issues, and algorithm insights.
- Do not use inline-comments to explain obvious code. Well written code is self-documenting.
- Only use comments for bad code to mention “fix this code” – otherwise remove, or rewrite the code!
- Include comments using Task-List keyword flags to permit comment-filtering.
Example:
// TODO: Place Database Code Here
// UNDONE: Removed P\Invoke Call due to errors
// HACK: Temporary fix until able to refactor - Only use C# comment-blocks for documenting the API.
- Always include comments.
If you have skills in PHP programming and you want to enhance your career in this field, a PHP certification from StudySection can help you reach your desired goals. Both beginner level and expert level PHP Certification Exams are offered by StudySection along with other programming certification exams.